; SUBROM.MAC

; MSX SUBROM, SONY HB-F1 (version 2.0)

; Source re-created by Z80DIS 2.2
; Z80DIS was written by Kenneth Gielow, Palo Alto, CA

; Code Copyrighted by ASCII and maybe others
; Source comments by Arjen Zeilemaker

; Sourcecode supplied for STUDY ONLY
; Recreation NOT permitted without authorisation of the copyrightholders
;
; Adaptado para ser montado pelo SJASMPLUS. Unificado codigo do MSX2 e MSX2+
; Usado constantes para os numeros de portas de I/O
; por Fabio Belavenuto e Leonard Oliveira @ 2014
;

	ORG		0

	DEFB	"CD"
	DEFW	C0336
	DEFW	C1649
	DEFW	C36D5


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

D0008:
	JP	J1D32

	DEFS	00010H-$,0

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

D0010:
	JP	J1D3A

	DEFS	00014H-$,0

D0014:
	JP	C0218

	DEFS	00018H-$,0

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

D0018:
	JP	J0577

	DEFS	0001CH-$,0

D001C:
	JP	C0262

	DEFS	00020H-$,0

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

D0020:
	LD	A,H
	SUB	D
	RET	NZ
	LD	A,L
	SUB	E
	RET

	DEFS	00028H-$,0

;	  Subroutine evaluate byte operand
;	     Inputs  ________________________
;	     Outputs ________________________

D0028:
	JP	C060E

	DEFS	00030H-$,0

D0030:
	JP	C0246

	DEFS	00038H-$,0

D0038:
	EX	AF,AF'
	EXX
	PUSH	AF
	PUSH	BC
	PUSH	DE
	PUSH	HL
	EX	AF,AF'
	EXX
	PUSH	IX
	PUSH	IY
	LD	IX,KEYINT
	LD	IY,(EXPTBL+0-1)
	CALL	C058D
	POP	IY
	POP	IX
	EX	AF,AF'
	EXX
	POP	HL
	POP	DE
	POP	BC
	POP	AF
	EX	AF,AF'
	EXX
	EI
	RETI

	DEFS	00066H-$,0

D0066:
	JP	H.NMI

S.PAINT:
	EI
	JP	J2651

S.PSET:
	EI
	JP	J2556

D0071:									; ATRSCN (Basic!)
	EI
	JP	C256F

S.GLINE:
	EI
	JP	J2607

D0079:									; DOBOXF (Basic!)
	EI
	JP	J262A

D007D:									; DOLINE (Basic!)
	EI
	JP	J263F

D0081:									; BOXLIN (Basic!)
	EI
	JP	J2648

S.DOGRPH:
	EI
	JP	C2823

S.GRPPRT:
	EI
	JP	C110E

D008D:									; SCALXY
	EI
	JP	C12C4

S.MAPXYC:
	EI
	JP	C1342

S.READC:
	EI
	JP	C1384

D0099:									; SETATR
	EI
	JP	C13AD

S.SETC:
	EI
	JP	C13B5

S.TRIGHT:
	EI
	JP	C1426

S.RIGHTC:
	EI
	JP	J1439

S.TLEFTC:
	EI
	JP	C13E7

S.LEFTC:
	EI
	JP	J144B

S.TDOWNC:
	EI
	JP	C13FC

S.DOWNC:
	EI
	JP	J148C

S.TUPC:
	EI
	JP	C1414

S.UPC:
	EI
	JP	J1498

S.SCANR:
	EI
	JP	C14DB

S.SCANL:
	EI
	JP	C158F

D00C9:									; NVBXLN
	EI
	JP	C27E7

D00CD:									; NVBXFL 
	EI
	JP	C2898

S.CHGMOD:
	EI
	JP	J09C3

S.INITXT:
	EI
	JP	C09E5

S.INIT32:
	EI
	JP	C0A1A

S.INIGRP:
	EI
	JP	C0A48

S.INIMLT:
	EI
	JP	C0A98

S.SETTXT:
	EI
	JP	C0B42

S.SETT32:
	EI
	JP	C0B9C

S.SETGRP:
	EI
	JP	C0BD2

S.SETMLT:
	EI
	JP	C0C22

S.CLRSPR:
	EI
	JP	C06F5

D00F9:									; CALPAT
	EI
	JP	J0755

D00FD:									; CALATR
	EI
	JP	C076A

D0101:									; GSPSIZ
	EI
	JP	C077F

D0105:									; GETPAT
	EI
	JP	C07D9

D0109:									; WTRVRM
	EI
	JP	C08CB

D010D:									; RDVRM
	EI
	JP	C08D6

D0111:									; CHGCLR
	EI
	JP	C0953

S.CLS:
	EI
	JP	C07FE

S.CLRTXT:
	EI
	JP	C0864

S.DSPFNK:
	EI
	JP	J0D52

S.DELLNO:
	EI
	JP	J0E0F

S.INSLNO:
	EI
	JP	J0E5D

D0129:									; PUTVRM
	EI
	JP	J0F16

S.WRTVDP:
	EI
	JP	C0647

D0131:									; VDPSTA
	EI
	JP	C298B

D0135:									; KYKLOK
	EI
	JP	J3F04

S.PUTCHR:
	EI
	JP	J3CC9

D013D:									; SETPAG
	EI
	JP	C06A8

D0141:									; INIPLT
	EI
	JP	J0F9F

D0145:									; RSTPLT
	EI
	JP	J0F7E

D0149:									; GETPLT
	EI
	JP	C0F6A

D014D:									; SETPLT
	EI
	JP	C0FD3

S.PUTSPRT:
	EI
	JP	J208F

S.COLOR:
	EI
	JP	J1DF6

S.SCREEN:
	EI
	JP	J1EE5

S.WIDTHS:
	EI
	JP	J1FDF

S.VDP:
	EI
	JP	J21C6

S.VDPF:
	EI
	JP	J21E5

S.BASE:
	EI
	JP	J2220

S.BASEF:
	EI
	JP	J2297

S.VPOKE:
	EI
	JP	J22F4

S.VPEEK:
	EI
	JP	J2307

S.SETS:
	EI
	JP	J1833

S.BEEP:
	EI
	JP	J1A62

S.PROMPT:
	EI
	JP	J1B4E

S.SDFSCR:
	EI
	JP	J0480

S.SETSCR:
	EI
	JP	J0470

S.SCOPY:
	EI
	JP	J2342

D0191:									; BLTVV
	EI
	JP	J2EB9

D0195:									; BLTVM
	EI
	JP	J2F42

D0199:									; BLTMV
	EI
	JP	J2EDF

D019D:									; BLTVD
	EI
	JP	J30A8

D01A1:									; BLTDV
	EI
	JP	J31B4

D01A5:									; BLTMD
	EI
	JP	J306F

D01A9:									; BLTDM
	EI
	JP	J307C

S.NEWPAD:
	EI
	JP	J34DD

S.GETPUT:
	EI
	JP	J1683

S.CHGMDP:
	EI
	JP	C09BF

D01B9:									; RESVI
	EI
	JP	0

D01BD:									; KNJPRT
	EI
	JP	J1049

;	Subroutine	calslt mainrom on page 0, when subrom is in a subslot of slot 0
;	Inputs		________________________
;	Outputs		________________________
;	Remark		The working of this code depends on the fact that the mainrom contains code
;			at 01D0H indentical to this code.

J01C1:	LD	A,B
	AND	3FH
	OUT	(PPI_PORT),A
	LD	A,(D.FFFF)
	CPL
	LD	C,A
	AND	D
	LD	E,A
	LD	(D.FFFF),A

; from this point code continues to execute in mainrom

D01D0:
	LD	A,B
	AND	D
	OUT	(PPI_PORT),A
	LD	A,E
	LD	(SLTTBL+0),A
	PUSH	BC
	EXX
	EX	AF,AF'
	CALL	CLPRM1
	DI
	EX	AF,AF'
	EXX
	POP	BC
	LD	A,B
	AND	3FH
	OUT	(PPI_PORT),A
	LD	A,C
	LD	(D.FFFF),A

; from this point code returns executing from the mainrom 

D01EB:
	LD	A,B
	OUT	(PPI_PORT),A
	LD	A,C
	LD	(SLTTBL+0),A
	EX	AF,AF'
	EXX
	RET

D01F5:	EI
	JP	C1D08

D01F9:	EI
	JP	C1D10

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C01FD:	CALL	C02DD			; calculate masks
	JP	M,J020D			; expanded slot,
	IN	A,(PPI_PORT)
	LD	D,A
	AND	C
	OR	B
	CALL	RDPRIM			; read from slot
	LD	A,E
	RET

J020D:	PUSH	HL
	CALL	C0302			; adjust secundairy slotregister
	EX	(SP),HL
	PUSH	BC
	CALL	C01FD			; RDSLT (on primairy slot)
	JR	J0233


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C0218:	PUSH	DE
	CALL	C02DD			; calculate masks
	JP	M,J0228			; expanded slot,
	POP	DE
	IN	A,(PPI_PORT)
	LD	D,A
	AND	C
	OR	B
	JP	WRPRIM			; write to slot

J0228:	EX	(SP),HL
	PUSH	HL
	CALL	C0302			; adjust secundairy slotregister
	POP	DE
	EX	(SP),HL
	PUSH	BC
	CALL	C0218			; WRSLT (on primairy slot)
J0233:	POP	BC
	EX	(SP),HL
	PUSH	AF
	LD	A,B
	AND	3FH
	OR	C
	OUT	(PPI_PORT),A
	LD	A,L
	LD	(D.FFFF),A
	LD	A,B
	OUT	(PPI_PORT),A
	POP	AF
	POP	HL
	RET

;	  Subroutine call mainrom subroutine (page 0)
;	     Inputs  ________________________
;	     Outputs ________________________
;	Remark	only works for subroutines in page 0

C0246:	EX	(SP),HL
	PUSH	AF
	PUSH	DE
	LD	A,(HL)
	PUSH	AF
	POP	IY
	INC	HL
	LD	E,(HL)
	INC	HL
	LD	D,(HL)
	INC	HL
	PUSH	DE
	POP	IX
	POP	DE
	POP	AF
	EX	(SP),HL

;	  Subroutine call mainrom subroutine (page 0)
;	     Inputs  ________________________
;	     Outputs ________________________
;	Remark	only works for subroutines in page 0

C0258:	EXX
	LD	D,0FCH			; page 0 only
	JR	J0265

;	  Subroutine call mainrom subroutine (page 1)
;	     Inputs  ________________________
;	     Outputs ________________________
;	Remark	only works for subroutines in page 1
;	Remark	NOT USED

D025D:	EXX
	LD	D,0F3H			; page 1 only
	JR	J0265


;	  Subroutine call mainrom subroutine (page 0 and 1)
;	     Inputs  ________________________
;	     Outputs ________________________

C0262:	EXX
	LD	D,0F0H			; page 0 and 1
J0265:	EX	AF,AF'
	DI
	LD	A,(EXPTBL+0)
	AND	A			; slot 0 expanded ?
	IN	A,(PPI_PORT)
	JP	M,J0276			; yep, take extra care
	PUSH	AF
	AND	D
	EXX
	JP	CLPRIM

J0276:	LD	B,A
	LD	A,(EXBRSA)
	AND	03H			; subrom in a subslot of slot 0 ?
	JR	NZ,J0284		; nope, handle it the normal way
	LD	A,D
	CP	0F3H			; page 1 only ?
	JP	NZ,J01C1		; nope, use a trick to make this work
J0284:	LD	A,B
	AND	3FH
	OUT	(PPI_PORT),A
	LD	A,(D.FFFF)
	CPL
	LD	C,A
	AND	D
	LD	(D.FFFF),A
	LD	E,A
	LD	A,B
	OUT	(PPI_PORT),A
	LD	A,E
	LD	(SLTTBL+0),A
	LD	A,B
	PUSH	BC
	LD	HL,I02A6
	PUSH	HL
	PUSH	AF
	AND	D
	EXX
	JP	CLPRIM

I02A6:	DI
	EXX
	EX	AF,AF'
	POP	BC
	LD	A,B
	AND	3FH
	OUT	(PPI_PORT),A
	LD	A,C
	LD	(D.FFFF),A
	LD	A,B
	OUT	(PPI_PORT),A
	LD	A,C
	LD	(SLTTBL+0),A
	EX	AF,AF'
	EXX
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C02BD:
	CALL	C02DD			; calculate masks
	JP	M,J02CA			; slot expanded,
	IN	A,(PPI_PORT)
	AND	C
	OR	B
	OUT	(PPI_PORT),A
	RET

J02CA:	PUSH	HL
	CALL	C0302			; adjust secundairy slotregister
	LD	C,A
	LD	B,00H
	LD	A,L
	AND	H
	OR	D
	LD	HL,SLTTBL
	ADD	HL,BC
	LD	(HL),A
	POP	HL
	LD	A,C
	JR	C02BD

;	  Subroutine calculate masks
;	     Inputs  ________________________
;	     Outputs ________________________

C02DD:	DI
	PUSH	AF
	LD	A,H
	RLCA
	RLCA
	AND	03H
	LD	E,A
	LD	A,0C0H
J02E7:	RLCA
	RLCA
	DEC	E
	JP	P,J02E7
	LD	E,A
	CPL
	LD	C,A
	POP	AF
	PUSH	AF
	AND	03H
	INC	A
	LD	B,A
	LD	A,0ABH
J02F8:	ADD	A,55H
	DJNZ	J02F8
	LD	D,A
	AND	E
	LD	B,A
	POP	AF
	AND	A
	RET

;	  Subroutine adjust secundairy slotregister
;	     Inputs  ________________________
;	     Outputs ________________________

C0302:	PUSH	AF
	LD	A,D
	AND	0C0H
	LD	C,A
	POP	AF
	PUSH	AF
	LD	D,A
	IN	A,(PPI_PORT)
	LD	B,A
	AND	3FH
	OR	C
	OUT	(PPI_PORT),A
	LD	A,D
	RRCA
	RRCA
	AND	03H
	LD	D,A
	LD	A,0ABH
J031A:	ADD	A,55H
	DEC	D
	JP	P,J031A
	AND	E
	LD	D,A
	LD	A,E
	CPL
	LD	H,A
	LD	A,(D.FFFF)
	CPL
	LD	L,A
	AND	H
	OR	D
	LD	(D.FFFF),A
	LD	A,B
	OUT	(PPI_PORT),A
	POP	AF
	AND	03H
	RET





;	  Subroutine subrom initialize
;	     Inputs  ________________________
;	     Outputs ________________________

C0336:
	LD	HL,I29F6
	LD	DE,RG0SAV
	LD	BC,8
	LDIR				; initialize vdp register 0-7 defaults
	LD	DE,RG8SAV
	LD	BC,16
	LDIR				; initialize vdp register 8-23 defaults
	XOR	A
	LD	(DPPAGE),A		; vdp display page=0
	LD	(ACPAGE),A		; vdp active page=0
	CALL	C0512			; get my slotid
	LD	(EXBRSA),A
	LD	H,A
	LD	L,0F7H
	LD	(H.ERRP+0),HL
	LD	HL,I3C11
	LD	(H.ERRP+2),HL		; expand error messages
	LD	A,(EXPTBL+0)
	LD	(D.FFF7),A
	LD	A,2CH
	LD	(XSAVE+1),A		; initialize X border skip lightpen
	LD	A,26H
	LD	(YSAVE+1),A		; initialize Y border skip lightpen
	LD	A,0D3H			; no external audio mixing, video input 21 pin, internal sync, AV control=1, Ym control=full, S1=0, S0=0
	OUT	(AV_PORT),A		; initialize digitizer hardware

	IF	CNTRY = 0
		LD	(AVCSAV),A
	ELSE
		CALL	C3CC3			; patch code european version
	ENDIF

	XOR	A

	IFNDEF	FS-A1FM	
		LD	C,0FFH			; assume internal devices should be enabled
	ELSE
		LD	C,0FDH
	ENDIF

	OUT	(KANJI1_PORT),A
	LD	A,2
	OUT	(KANJI1_PORT+1),A
	LD	HL,I050A
	LD	B,8
J0387:
	IN	A,(KANJI1_PORT+1)
	CP	(HL)
	JR	NZ,J0391
	INC	HL
	DJNZ	J0387
	RES	0,C			; JIS-1 rom found, disable internal JIS-1
J0391:
	IN	A,(FM_AUDIO)
	CP	0FFH
	JR	Z,J0399
	RES	2,C			; MSX-AUDIO found, disable internal MSX-AUDIO
J0399:
	IN	A,(AV_PORT)
	AND	77H			; b6-b4, b2-b0 are outputs which are readback, b7 and b3 are I/O with different input function
	CP	77H
	JR	Z,J03A3
	RES	3,C			; superimpose found, disable internal superimpose
J03A3:
	IN	A,(MSXIF_PORT)
	CP	0FFH
	JR	Z,J03AB
	RES	4,C			; MSX-MOTOR found, disable internal MSX-MOTOR interface
J03AB:
	XOR	A
	OUT	(UART_PORT+1),A
	PUSH	AF
	POP	AF
	OUT	(UART_PORT+1),A
	PUSH	AF
	POP	AF
	OUT	(UART_PORT+1),A
	PUSH	AF
	POP	AF
	LD	A,40H
	OUT	(UART_PORT+1),A
	EX	(SP),HL
	EX	(SP),HL
	IN	A,(UART_PORT+1)
	AND	3FH
	CP	05H
	JR	NZ,J03C8
	RES	5,C			; MSX-RS232 found, disable internal MSX-RS232 interface
J03C8:
	IN	A,(LIGPEN_PORT+3)
	CP	0FFH
	JR	Z,J03D0
	RES	6,C			; lightpen found, disable internal lightpen interface
J03D0:
	LD	A,C
	OUT	(SYS_PORT),A		; enable internal devices

	call	C1CF7						; select RP-501 bank 2
	ld		b, 0
	call	TESTRTC						; RTC exist?
	jr nz,	NORTC						; no, jump initial tests
	cp		$0A							; RTC exist, is it initialized?
	jr z,	J03E2						; yes, normal tests
J03DF:
	CALL	C0431			; initalize RP-501
J03E2:
	CALL	C1CF7			; select RP-501 bank 2
	LD	B, 3
	CALL	READRTC			; read RP-501 register 3
	AND	01H
	PUSH	AF
	CALL	C1CDB			; read byte from RP-501 registers 4/5
	POP	BC
	CP	80+1			; screenwidth >80 ?
	JP	NC,J03DF		; yep, initialize
	OR	A			; screenwidth <1 ?
	JP	Z,J03DF			; yep, initialize
	DEC	B			; screenmode 1 ?
	JR	NZ,J0402		; nope,
	CP	32+1			; screenwidth >32 ?
	JP	NC,J03DF		; yep, initialize
J0402:
	CALL	C1CF7			; select RP-501 bank 2
	LD	B,1
	CALL	READRTC			; read RP-501 register 1
	LD	D,A
	CALL	READRTC			; read RP-501 register 2
	LD	E,A
	CALL	C17F8			; setup screenposition
NORTC:
	CALL	C2A0E			; display msx startscreen
	LD	B,2
	LD	HL,0
J041A:
	DEC	HL
	LD	A,L
	OR	H
	JR	NZ,J041A
	DJNZ	J041A			; wait
	CALL	C063C			; disable screendisplay
	XOR	A
	LD	BC,04000H
	CALL	C0977			; fill VRAM (active page)
	CALL	C0A1A			; setup screen 1 (INIT32)
	JP	J0F9F			; initialize pallette (INIPLT)

; by Fabio Belavenuto - test if RTC exists

TESTRTC:
	push	bc
	ld		a, b						; select RTC register
	out		(RTC_PORT), a
	in		a, (RTC_PORT+1)				; read RTC register 0
	and		$0F
	ld		c, a						; save in C
	cpl									; invert
	and		$0F
	out		(RTC_PORT+1), a				; write RTC
	in		a, (RTC_PORT+1)				; read value
	cpl									; invert
	and		$0F
	out		(RTC_PORT+1), a				; write RTC
	cp		c							; same value?
	pop		bc
	ret

;	  Subroutine initalize RP-501
;	     Inputs  ________________________
;	     Outputs ________________________

C0431:
	LD	HL,I04FF
	LD	C,2			; 2 banks
J0436:
	LD	B,13			; bank register
	LD	A,4
	SUB	C			; bank 2 or 3, alarm output OFF, CLOCK count stop
	CALL	C1A59			; write nibble to RP-501 (register 13)
J043E:
	LD	B,13
J0440:
	LD	A,13
	SUB	B
	OUT	(RTC_PORT),A		; select RP-501 register
	LD	A,(HL)
	INC	HL
	CP	0FFH
	JR	NZ,J044D
	DEC	HL
	XOR	A
J044D:
	OUT	(RTC_PORT+1),A		; write RP-501 register
	DJNZ	J0440			; next register
	DEC	C
	JR	NZ,J0436		; next bank
	LD	B,13
	LD	A,08H			; bank 0, alarm output OFF, CLOCK count start
	CALL	C1A59			; write nibble to RP-501 (register 13)
	XOR	A			; all tests disabled
	CALL	C1A59			; write nibble to RP-501 (register 14)
	LD	A,0DH			; reset alarm registers,  leave fractions, 16 Hz pulse off, 1 Hz pulse off
	CALL	C1A59			; write nibble to RP-501 (register 15)
	LD	A,1
	CALL	C1CF9			; select RP-501 bank 1
	LD	B,10
	LD	A,1			; 24-hour clock
	JP	C1A59			; write nibble to RP-501 (register 10)

J0470:
	SCF				; set functionkey display according to startup value
	CALL	C0481			; restore saved settings
	LD	HL,T7EF2
	CALL	C05F6			; message to interpreter output
	LD	HL,T7EE4
	JP	C05F6			; message to interpreter output

;	  Subroutine restore saved settings, but leave functionkey display off
;	     Inputs  ________________________
;	     Outputs ________________________

J0480:
	XOR	A			; force functionkey display off

;	  Subroutine restore saved settings
;	     Inputs  ________________________
;	     Outputs ________________________

C0481:
	PUSH	AF
	CALL	C1CF7			; select RP-501 bank 2
	LD	B,3
	CALL	READRTC			; read RP-501 register 3
	LD	C,A
	AND	01H			; startup screenmode
	POP	DE
	PUSH	AF
	PUSH	DE
	PUSH	BC
	LD	A,C
	RLCA
	RLCA
	AND	08H
	LD	C,A			; startup interlace mode in b3
	LD	A,(RG9SAV)
	AND	0F7H
	OR	C
	LD	B,A
	LD	C,9
	CALL	C0647			; write vdp register 9
	POP	BC
	CALL	C1CDB			; read byte from RP-501 registers 4/5
	LD	E,A			; startup screenwidth
	POP	AF
	PUSH	DE
	PUSH	AF
	CALL	READRTC			; read RP-501 register 6
	LD	(FORCLR),A		; set foreground color
	LD	(ATRBYT),A		; set attribute color
	CALL	READRTC			; read RP-501 register 7
	LD	(BAKCLR),A		; set background color
	CALL	READRTC			; read RP-501 register 8
	LD	(BDRCLR),A		; set border color
	CALL	READRTC			; read RP-501 register 9
	LD	C,A
	RRC	C			; functionkey display on/off
	SBC	A,A
	LD	B,A
	POP	AF			; functionkey display forced off ?
	JR	NC,J04CC		; yep,
	LD	A,B
J04CC:
	LD	(CNSDFG),A
	RRC	C			; keyclick on/off
	SBC	A,A
	LD	(CLIKSW),A

	IF CSPRFIX = 1

	BIT	1,C			; cassette speed
	PUSH	BC
	LD	BC,5
	LD	HL,CS1200
	JR	Z,J04E3
	LD	HL,CS2400
J04E3:
	LD	DE,LOWx
	LDIR
	POP	BC
	RRC	C			; printermode
	SBC	A,A
	LD	(NTMSXP),A

	ELSE
	
	RRC	C			; cassette speed (BUG, it is the printermode)
	PUSH	BC
	LD	BC,5
	LD	HL,CS1200
	JR	NC,J04E3
	LD	HL,CS2400
J04E3:
	LD	DE,LOWx
	LDIR
	POP	BC
	RRC	C			; printermode (BUG, it is the cassette speed)
	SBC	A,A
	LD	(NTMSXP),A

	ENDIF

	POP	DE
	POP	AF			; startup screenmode
	LD	(OLDSCR),A
	LD	(SCRMOD),A
	PUSH	DE
	CALL	C09BF			; change screenmode, initialize pallette
	POP	DE
	JP	J1FFF

; Initial RP-501 data

I04FF:
	DEFB	$0A					; id
	DEFB	$00					; adjust X
	DEFB	$00					; adjust Y

	IF BASVER = 0
		DEFB	$01				; no interlace, screen 1
		DEFB	29 MOD 16
		DEFB	29 / 16			; screen width
	ELSE
		DEFB	$00				; no interlace, screen 0
		DEFB	37 MOD 16
		DEFB	37 / 16			; screen width
	ENDIF

	DEFB	$0F					; foreground color
	DEFB	$04					; background color

	IF BASVER = 0
		DEFB	$07				; border color
	ELSE
		DEFB	$04				; border color
	ENDIF

	DEFB	$03					; cassette speed 1200, printer mode msx, key click on, function keys on

	IF CNTRY != 0
		DEFB	$01				; beep tone 0, volume 1
		DEFB	$00				; title color
		DEFB	CNTRY			; native code
	ENDIF

	DEFB	$FF

; JIS-1 pattern

I050A:
	DEFB	00H,40H,20H,10H,08H,04H,02H,01H

;	  Subroutine get my slotid
;	     Inputs  ________________________
;	     Outputs ________________________

C0512:
	PUSH	BC
	PUSH	HL
	IN	A,(PPI_PORT)
	AND	03H
	LD	C,A
	LD	B,00H
	LD	HL,EXPTBL
	ADD	HL,BC
	OR	(HL)
	LD	C,A
	INC	HL
	INC	HL
	INC	HL
	INC	HL
	LD	A,(HL)
	AND	03H
	RLCA
	RLCA
	OR	C
	POP	HL
	POP	BC
	RET

J052E:
	LD	IX,J6E6B
	JR	J0562

J0534:
	LD	IX,J6E86
	JR	J0562

J053A:
	LD	IX,J6E6E
	JR	J0562

J0540:
	LD	IX,J4055
	JR	J0562

J0546:
	LD	IX,C475A
	JR	J0562

J054C:
	LD	IX,J406A
	JR	J0562

J0552:
	LD	IX,J406D
	JR	J0562

J0558:
	LD	IX,J6275
	JR	J0562

J055E:
	LD	IX,J6E74
J0562:
	JP	J0618

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C0565:
	LD	IX,CHGET
	JR	C058D


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C056B:
	LD	IX,GICINI
	JR	C058D

J0571:
	LD	IX,FNKSB
	JR	C058D

J0577:
	LD	IX,OUTDO
	JR	C058D


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C057D:
	LD	IX,RDSLT
	JR	C058D


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C0583:
	LD	IX,WRSLT
	JR	C058D


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C0589:
	LD	IX,CKCNTC

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C058D:
	JP	C0258


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C0590:
	LD	IX,SNSMAT
	JR	C058D

J0596:
	LD	IX,EOL
	JR	C058D

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C059C:
	LD	IX,C579C
	JR	J0618


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C05A2:
	LD	IX,C57AB
	JR	J0618

J05A8:
	LD	IX,C7328
	JR	J05F4


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C05AE:
	LD	IX,C7323
	JR	J05F4


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C05B4:
	LD	IX,C5439
	JR	J05F4


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C05BA:
	LD	IX,C6A0E
	JR	J05F4


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C05C0:
	LD	IX,C6B24
	JR	J05F4

J05C6:
	LD	IX,J406F
	JR	J05F4


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C05CC:
	LD	IX,C3412
	JR	J05F4


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C05D2:
	LD	IX,C3058
	JR	J05F4


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C05D8:
	LD	IX,C5EA4
	JR	J05F4


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C05DE:
	LD	IX,C3236
	JR	J05F4


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C05E4:
	LD	IX,C4FCF
	JR	J05F4


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C05EA:
	LD	IX,C6627
	JR	J05F4

J05F0:
	LD	IX,C2F92
J05F4:
	JR	J0618


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C05F6:
	LD	IX,C6678
	JR	J05F4


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C05FC:
	LD	IX,C4C64
	JR	J0618


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C0602:
	LD	IX,C542F
	JR	J0618


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C0608:
	LD	IX,C520F
	JR	J0618


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C060E:
	LD	IX,C521C
	JR	J0618


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C0614:
	LD	IX,C67D0
J0618:
	CALL	C0262
	EI
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C061D:
	LD	(CNSDFG),A

;	  Subroutine vdp in textmode ?
;	     Inputs  ________________________
;	     Outputs ________________________

C0620:
	LD	A,(SCRMOD)
	CP	2
	RET


;	  Subroutine vdp in MSX1 style screenmode ?
;	     Inputs  ________________________
;	     Outputs ________________________

C0626:
	LD	A,(SCRMOD)
	CP	4
	RET


;	  Subroutine vdp in new style screenmode ?
;	     Inputs  ________________________
;	     Outputs ________________________

C062C:
	LD	A,(SCRMOD)
	CP	5
	RET

;	  Subroutine enable screendisplay
;	     Inputs  ________________________
;	     Outputs ________________________

J0632:
	CALL	C1034			; wait for VR to pass
	LD	A,(RG1SAV)
	OR	60H			; IE0=1, BL=1 (enable VR interrupts, enable screen)
	JR	J0644


;	  Subroutine disable screendisplay
;	     Inputs  ________________________
;	     Outputs ________________________

C063C:
	CALL	C1034			; wait for VR to pass
	LD	A,(RG1SAV)
	AND	3FH			; b7=0, BL=0 (reset unused bit (TMS9918->4K/16K), disable screen)
J0644:
	LD	B,A
	LD	C,1			; vdp register 1

;	  Subroutine write vdp register (S.WRTVDP)
;	     Inputs  ________________________
;	     Outputs ________________________

C0647:	LD	A,I
	PUSH	AF
	PUSH	HL
	LD	A,C
	AND	A			; vdp register 0 ?
	JR	NZ,J0680		; nope, change vdp register
	LD	A,B
	LD	HL,RG0SAV
	XOR	(HL)
	AND	01H			; b0 (TMS 9918: external VDP input) changed ?
	JR	Z,J0680			; nope, change vdp register
	LD	HL,RG9SAV
	LD	A,(HL)
	AND	0CFH
	LD	(HL),A			; reset b5,b4 (S1,S0) -> normal MSX-VIDEO
	LD	A,B
	AND	01H
	RRCA
	RRCA
	RRCA
	RRCA
	OR	(HL)			; if TMS 9918 external VDP input then select Superimpose MSX-VIDEO (transparent parts)
	PUSH	BC
	LD	B,A			; new value register 9
	LD	C,9			; vdp register 9
	LD	HL,AVCSAV
	LD	A,(HL)
	AND	3FH
	LD	(HL),A			; reset b7,b6
	LD	A,B
	RLCA
	RLCA
	CPL
	AND	0C0H
	OR	(HL)
	OUT	(AV_PORT),A
	LD	(HL),A
	CALL	C0647			; write vdp register 9
	POP	BC
J0680:	LD	A,B
	DI
	OUT	(VDP_PORT+1),A
	LD	A,C
	OR	80H
	OUT	(VDP_PORT+1),A
	PUSH	BC
	PUSH	DE
	LD	D,B
	LD	A,C
	LD	B,0
	CP	08H
	JR	NC,J0698
	LD	HL,RG0SAV
	JR	J069F

J0698:	CP	18H
	JR	NC,J06A1
	LD	HL,RG8SAV-8
J069F:	ADD	HL,BC
	LD	(HL),D
J06A1:	POP	DE
	POP	BC
	POP	HL
	POP	AF
	RET	PO
	EI
	RET

;	  Subroutine set active/display page
;	     Inputs  ________________________
;	     Outputs ________________________

C06A8:	LD	A,(SCRMOD)
	PUSH	AF
	PUSH	BC
	PUSH	DE
	PUSH	HL
	DI
	LD	HL,I06B8
	CALL	C09C9
	JR	J06F0

I06B8:	DEFW	C0B42			; SETTXT
	DEFW	C0B9C			; SETT32
	DEFW	C0BD2			; SETGRP
	DEFW	C0C22			; SETMLT
	DEFW	C0BD9			; setup for GRAPHIC 3 mode (screenmode 4)
	DEFW	C0C56			; setup for GRAPHIC 4 mode (screenmode 5)
	DEFW	C0C80			; setup for GRAPHIC 5 mode (screenmode 6)
	DEFW	C0C97			; setup for GRAPHIC 6 mode (screenmode 7)
	DEFW	C0CC7			; setup for GRAPHIC 7 mode (screenmode 8)

;	  Subroutine update vdp registers from backup
;	     Inputs  ________________________
;	     Outputs ________________________

J06CA:	PUSH	AF
	PUSH	BC
	PUSH	DE
	PUSH	HL
	DI
	XOR	A
	OUT	(VDP_PORT+1),A
	LD	A,080H+17
	OUT	(VDP_PORT+1),A			; set indirect vdp register pointer to 0, autoincrement
	CALL	C1034			; wait for VR to pass
	LD	HL,RG0SAV
	LD	B,8
J06DE:	LD	A,(HL)
	INC	HL
	OUT	(VDP_PORT+3),A
	DJNZ	J06DE
	LD	HL,RG8SAV
	LD	B,16
J06E9:	LD	A,(HL)
	INC	HL
	OUT	(VDP_PORT+3),A
	DJNZ	J06E9
	EI
J06F0:	POP	HL
	POP	DE
	POP	BC
	POP	AF
	RET

;	  Subroutine clear sprites (CLRSPR)
;	     Inputs  ________________________
;	     Outputs ________________________

C06F5:	CALL	C0701			; initialize sprites
	PUSH	HL
	LD	HL,0
	CALL	C08E3			; set VRAM pointer for writing (active page)
	POP	HL
	RET


;	  Subroutine initialize sprites
;	     Inputs  ________________________
;	     Outputs ________________________

C0701:	LD	A,(RG1SAV)
	LD	B,A
	LD	C,1
	CALL	C0647			; write vdp register 1
	LD	HL,(PATBAS)
	LD	BC,256*8
	XOR	A
	CALL	C0977			; clear sprite patterns (fill VRAM (active page))

;	  Subroutine initialize spriteplanes and spritecolor table
;	     Inputs  ________________________
;	     Outputs ________________________

C0714:	LD	A,(FORCLR)
	LD	E,A
	LD	HL,(ATRBAS)
	LD	BC,02000H		; 32 sprites, start with sprite 0
J071E:	CALL	C0626			; MSX1 screenmode ?
	LD	A,0D1H
	JR	C,J0727			; yep,
	LD	A,0D9H
J0727:	CALL	C08CB			; set sprite Y-coord (write VRAM (active page))
	INC	HL
	INC	HL			; leave sprite X-coord alone
	LD	A,C
	CALL	C08CB			; set sprite pattern (write VRAM (active page))
	INC	HL
	INC	C
	LD	A,(RG1SAV)
	RRCA
	RRCA				; 16*16 sprites ?
	JR	NC,J073C		; nope,
	INC	C
	INC	C
	INC	C			; yep, sprite+4
J073C:	LD	A,E
	CALL	C08CB			; set sprite color (write VRAM (active page))
	INC	HL
	DJNZ	J071E
	CALL	C0626			; MSX1 screenmode ?
	RET	C			; yep, quit
	LD	HL,(ATRBAS)
	LD	BC,512
	SBC	HL,BC
	LD	A,(FORCLR)
	JP	C0977			; initialize sprite colortable (fill VRAM (active page))

;	  Subroutine get spritepattern address
;	     Inputs  ________________________
;	     Outputs ________________________

J0755:	LD	L,A
	LD	H,0
	ADD	HL,HL
	ADD	HL,HL
	ADD	HL,HL
	CALL	C077F			; get spritesize
	CP	8			; 16*16 sprites ?
	JR	Z,J0764			; nope,
	ADD	HL,HL
	ADD	HL,HL
J0764:	EX	DE,HL
	LD	HL,(PATBAS)
	ADD	HL,DE
	RET

;	  Subroutine get spriteplane address
;	     Inputs  ________________________
;	     Outputs ________________________

C076A:	LD	L,A
	LD	H,0
	ADD	HL,HL
	ADD	HL,HL
	EX	DE,HL
	LD	HL,(ATRBAS)
	ADD	HL,DE
	RET


;	  Subroutine get spriteplane colortable
;	     Inputs  ________________________
;	     Outputs ________________________

C0775:	ADD	A,A
	ADD	A,A
	CALL	C076A			; get spritelane address
	LD	DE,-512
	ADD	HL,DE
	RET


;	  Subroutine get spritesize
;	     Inputs  ________________________
;	     Outputs ________________________

C077F:	LD	A,(RG1SAV)
	RRCA
	RRCA
	LD	A,8
	RET	NC
	LD	A,32
	RET

;	  Subroutine initialize characterpatterns
;	     Inputs  ________________________
;	     Outputs ________________________

C078A:	CALL	H.INIP
	LD	HL,(CGPBAS)
	CALL	C08E3			; set VRAM pointer for writing (active page)
	LD	A,(CGPNT+0)		; slotid charset
	LD	HL,(CGPNT+1)		; address charset
	LD	BC,256*8
	PUSH	AF
J079D:	POP	AF
	PUSH	AF
	PUSH	BC
	DI
	CALL	C057D			; call RDSLT mainrom
	EI
	POP	BC
	OUT	(VDP_PORT),A
	INC	HL
	DEC	BC
	LD	A,C
	OR	B
	JR	NZ,J079D
	POP	AF
	RET

;	  Subroutine copy vram to memory
;	     Inputs  ________________________
;	     Outputs ________________________
;	Remark	NOT USED

D07B0:	CALL	C08F1			; set VRAM pointer for reading (active page)
	LD	A,C
	OR	A
	LD	A,B
	LD	B,C
	LD	C,VDP_PORT
	EX	DE,HL
	JR	Z,J07BD
	INC	A			; setup for INIR
J07BD:	INIR
	DEC	A
	JR	NZ,J07BD
	EX	DE,HL
	RET

;	  Subroutine copy memory to vram
;	     Inputs  ________________________
;	     Outputs ________________________
;	Remark	NOT USED

D07C4:	EX	DE,HL
	CALL	C08E3			; set VRAM pointer for writing (active page)
	EX	DE,HL
	LD	A,C
	OR	A
	LD	A,B
	LD	B,C
	LD	C,VDP_PORT
	JR	Z,J07D2
	INC	A			; setup for OTIR
J07D2:	OTIR
	DEC	A
	JR	NZ,J07D2
	EX	DE,HL
	RET


;	  Subroutine copy characterpattern to PATWRK
;	     Inputs  ________________________
;	     Outputs ________________________

C07D9:	LD	H,0
	LD	L,A
	ADD	HL,HL
	ADD	HL,HL
	ADD	HL,HL			; *8
	EX	DE,HL
	LD	HL,(CGPNT+1)
	ADD	HL,DE
	LD	DE,PATWRK
	LD	B,8
	LD	A,(CGPNT+0)
J07EC:	PUSH	AF
	PUSH	HL
	PUSH	DE
	PUSH	BC
	CALL	C057D			; call RDSLT mainrom
	EI
	POP	BC
	POP	DE
	POP	HL
	LD	(DE),A
	INC	DE
	INC	HL
	POP	AF
	DJNZ	J07EC
	RET


;	  Subroutine clear screen (CLS)
;	     Inputs  ________________________
;	     Outputs ________________________

C07FE:	CALL	C0626			; vdp in MSX1 style screenmode ?
	JP	Z,C0895			; screenmode 4, clear screen in screenmode 2/4
	JR	NC,C0811		; new style MSX2 screenmode, clear screen in new style MSX2 screenmode
	CALL	C0620			; vdp in textmode ?
	JP	Z,C0895			; graphics mode 2, clear screen in screenmode 2/4
	JP	NC,C08AD		; graphics mode 3, clear screen in screenmode 3
	JR	C0864			; clear screen in textmode

;	  Subroutine clear screen in new style MSX2 screenmode
;	     Inputs  ________________________
;	     Outputs ________________________

C0811:	PUSH	AF
	PUSH	BC
	PUSH	DE
	PUSH	HL
	CALL	C2980			; wait for vdp command to complete
	LD	A,(BAKCLR)
	CALL	C084B			; convert color to colorregister value
	CALL	C29EB			; write colorregister
	XOR	A
	LD	H,A
	LD	L,A
	CALL	C29A5			; write DX,DY registers
	XOR	A
	CALL	C29DB			; write ARG register
	LD	A,(SCRMOD)
	AND	02H			; screenmode 6 or 7 ?
	LD	DE,256
	JR	Z,J0838			; nope, screenwidth 256 pixels
	LD	DE,512			; yep, screenwidth 512 pixels
J0838:	LD	HL,212
	CALL	C29C4			; write NX,NY registers
	LD	A,0C0H			; HMMV (high speed move) command
	CALL	C29E3			; execute vdp command
	CALL	C2980			; wait for vdp command to complete
	POP	HL
	POP	DE
	POP	BC
	POP	AF
	RET


;	  Subroutine convert color to colorregister value
;	     Inputs  ________________________
;	     Outputs ________________________

C084B:	LD	B,A
	LD	A,(SCRMOD)
	CP	8			; screenmode 8 ?
	LD	A,B
	RET	Z			; yep, use all 8 bits and quit
	RLCA
	RLCA
	RLCA
	RLCA
	OR	B
	LD	B,A			; copy 4 lower bits in 4 higher bits
	LD	A,(SCRMOD)
	CP	6			; screenmode 6 ?
	LD	A,B
	RET	NZ			; nope, quit
	RRCA
	RRCA
	OR	B			; copy 2 lower bits in b2-b3, b4-b5 and b6-b7
	RET

;	  Subroutine clear screen in textmode
;	     Inputs  ________________________
;	     Outputs ________________________

C0864:	LD	A,(SCRMOD)
	AND	A			; screenmode 0 ?
	LD	HL,(NAMBAS)
	JR	NZ,J087E		; nope, clear in screenmode 1
	LD	A,(LINLEN)
	CP	40+1
	JR	NC,J0879
	LD	BC,40*24
	JR	J0881

J0879:	LD	BC,80*24
	JR	J0881

J087E:	LD	BC,32*24
J0881:	LD	A,20H
	CALL	C0977			; fill VRAM (active page)
	CALL	C08C1			; cursorposition to home
	LD	HL,LINTTB
	LD	B,24
J088E:	LD	(HL),B
	INC	HL
	DJNZ	J088E			; initialize LINTTB
	JP	J0571			; handle functionkey display

;	  Subroutine clear screen in screenmode 2/4
;	     Inputs  ________________________
;	     Outputs ________________________

C0895:	CALL	C099C			; write bordercolor register
	LD	BC,1800H
	PUSH	BC
	LD	HL,(GRPCOL)
	LD	A,(BAKCLR)
	CALL	C0977			; fill VRAM (active page)
	LD	HL,(GRPCGP)
	POP	BC
	XOR	A
J08AA:	JP	C0977			; fill VRAM (active page)


;	  Subroutine clear screen in screenmode 3
;	     Inputs  ________________________
;	     Outputs ________________________

C08AD:	CALL	C099C			; write bordercolor register
	LD	HL,BAKCLR
	LD	A,(HL)
	ADD	A,A
	ADD	A,A
	ADD	A,A
	ADD	A,A
	OR	(HL)
	LD	HL,(MLTCGP)
	LD	BC,0600H
	JR	J08AA


;	  Subroutine cursorposition to home
;	     Inputs  ________________________
;	     Outputs ________________________

C08C1:	LD	HL,00101H
	LD	(CSRY),HL
	RET

;	  Subroutine write VRAM limit 16 Kb (active page)
;	     Inputs  ________________________
;	     Outputs ________________________
;	Remark	NOT USED

D08C8:	CALL	C08DC			; adjust to 16 KB range

;	  Subroutine write VRAM (active page)
;	     Inputs  ________________________
;	     Outputs ________________________

C08CB:	PUSH	AF
	CALL	C08E3			; set VRAM pointer for writing (active page)
	POP	AF
	OUT	(VDP_PORT),A
	RET


;	  Subroutine read VRAM limit 16 Kb (active page)
;	     Inputs  ________________________
;	     Outputs ________________________

C08D3:	CALL	C08DC			; adjust to 16 KB range

;	  Subroutine read VRAM (active page)
;	     Inputs  ________________________
;	     Outputs ________________________

C08D6:	CALL	C08F1			; set VRAM pointer for reading (active page)
	IN	A,(VDP_PORT)
	RET


;	  Subroutine adjust to 16 KB range
;	     Inputs  ________________________
;	     Outputs ________________________

C08DC:	PUSH	AF
	LD	A,H
	AND	3FH
	LD	H,A
	POP	AF
	RET

;	  Subroutine set VRAM pointer for writing (active page)
;	     Inputs  ________________________
;	     Outputs ________________________

C08E3:	PUSH	BC
	PUSH	DE
	PUSH	HL
	EX	DE,HL
	CALL	C0914			; convert to absolute VRAM address
	LD	A,H
	AND	3FH
	OR	40H
	JR	J08FB


;	  Subroutine set VRAM pointer for reading (active page)
;	     Inputs  ________________________
;	     Outputs ________________________

C08F1:	PUSH	BC
	PUSH	DE
	PUSH	HL
	EX	DE,HL
	CALL	C0914			; convert to absolute VRAM address
	LD	A,H
	AND	3FH
J08FB:	PUSH	AF
	LD	A,H
	AND	0C0H
	OR	D
	RLCA
	RLCA
	DI
	OUT	(VDP_PORT+1),A
	LD	A,080H+14
	OUT	(VDP_PORT+1),A
	LD	A,L
	OUT	(VDP_PORT+1),A
	POP	AF
	OUT	(VDP_PORT+1),A
	EI
	POP	HL
	POP	DE
	POP	BC
	RET

;	  Subroutine convert to absolute VRAM address
;	     Inputs  ________________________
;	     Outputs ________________________

C0914:	LD	A,(SCRMOD)
	LD	C,A			; current screenmode
	LD	A,(ACPAGE)
	AND	A			; active page is 0 ?
	LD	HL,I0925
	JP	NZ,J09CA		; nope,
	EX	DE,HL
	LD	D,A			; D=0 (b23-b16) VRAM address
	RET

I0925:	DEFW	C0937			; screenmode 0
	DEFW	C0945			; screenmode 1, 16 Kb per page
	DEFW	C0945			; screenmode 2, 16 Kb per page
	DEFW	C0945			; screenmode 3, 16 Kb per page
	DEFW	C0945			; screenmode 4, 16 Kb per page
	DEFW	C0944			; screenmode 5, 32 Kb per page
	DEFW	C0944			; screenmode 6, 32 Kb per page
	DEFW	C0943			; screenmode 7, 64 Kb per page
	DEFW	C0943 			; screenmode 8, 64 Kb per page

C0937:	LD	A,(LINLEN)
	CP	40+1			; 40 col mode ?
	LD	A,(ACPAGE)
	JR	C,J0947			; yep, 4 Kb per page
	JR	J0946			; nope, 8 Kb per page

C0943:	ADD	A,A
C0944:	ADD	A,A
C0945:	ADD	A,A
J0946:	ADD	A,A
J0947:	ADD	A,A
	ADD	A,A
	ADD	A,A
	ADD	A,A
	LD	H,A
	LD	A,0
	LD	L,A			; b7-b0 = 0
	ADC	A,A			; b16
	ADD	HL,DE
	LD	D,A			; b23-b16
	RET


;	  Subroutine change colors (CHGCLR)
;	     Inputs  ________________________
;	     Outputs ________________________

C0953:	CALL	C062C			; new style screenmode ?
	JP	NC,J0997		; yep,
	CALL	C09A5			; adjust forground and background color to 4 bits
	DEC	A			; screenmode 0 ?
	JP	M,J098A			; yep,
	PUSH	AF
	CALL	C099C			; write bordercolor register
	POP	AF
	RET	NZ			; screenmode 2 or 3, quit
	LD	HL,BAKCLR
	LD	A,(FORCLR)
	ADD	A,A
	ADD	A,A
	ADD	A,A
	ADD	A,A
	OR	(HL)
	LD	HL,(T32COL)
	LD	BC,32

;	  Subroutine fill VRAM (active page)
;	     Inputs  ________________________
;	     Outputs ________________________

C0977:	PUSH	AF
	CALL	C08E3			; set VRAM pointer for writing (active page)
	LD	A,C
	OR	A
	JR	Z,J0980
	INC	B
J0980:	POP	AF
J0981:	OUT	(VDP_PORT),A
	DEC	C
	JP	NZ,J0981
	DJNZ	J0981
	RET

J098A:	LD	HL,BAKCLR
	LD	A,(FORCLR)
	ADD	A,A
	ADD	A,A
	ADD	A,A
	ADD	A,A
	OR	(HL)
	JR	J099F

J0997:	CP	8
	CALL	NZ,C09A5		; nope, adjust forground and background color to 4 bits

;	  Subroutine write bordercolor register
;	     Inputs  ________________________
;	     Outputs ________________________

C099C:	LD	A,(BDRCLR)
J099F:	LD	B,A
	LD	C,7
	JP	C0647			; write vdp register 7


;	  Subroutine adjust forground and background color to 4 bits
;	     Inputs  ________________________
;	     Outputs ________________________

C09A5:	PUSH	AF
	LD	A,(BAKCLR)
	AND	0FH
	LD	(BAKCLR),A
	LD	A,(FORCLR)
	AND	0FH
	LD	(FORCLR),A
	POP	AF
	RET

;	  Subroutine CLS statement
;	     Inputs  ________________________
;	     Outputs ________________________
;	Remark	NOT USED

D09B8:	RET	NZ
	PUSH	HL
	CALL	C07FE			; clear screen
	POP	HL
	RET


;	  Subroutine change screenmode, initialize pallette (CHGMDP)
;	     Inputs  ________________________
;	     Outputs ________________________

C09BF:	LD	HL,J0F9F
	PUSH	HL			; initialize pallette afterwards

;	  Subroutine change screenmode
;	     Inputs  ________________________
;	     Outputs ________________________

J09C3:	CP	9			; screenmode valid ?
	RET	NC			; nope, quit
	LD	HL,I09D3

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C09C9:	LD	C,A
J09CA:	LD	B,0
	ADD	HL,BC
	ADD	HL,BC
	LD	C,(HL)
	INC	HL
	LD	H,(HL)
	LD	L,C
	JP	(HL)

I09D3:	DEFW	C09E5			; INITXT
	DEFW	C0A1A			; INIT32
	DEFW	C0A48			; INIGRP
	DEFW	C0A98			; INIMLT
	DEFW	C0A71			; initialize screenmode 4
	DEFW	C0AD6			; initialize screenmode 5
	DEFW	C0AE3			; initialize screenmode 6
	DEFW	C0AF0			; initialize screenmode 7
	DEFW	C0AFD			; initialize screenmode 8

;	  Subroutine INITXT
;	     Inputs  ________________________
;	     Outputs ________________________

C09E5:	XOR	A
	LD	(OLDSCR),A		; force to textmode reverts to screenmode 0
	CALL	C0B22			; setup for screenmode change
	LD	A,(LINL40)
	LD	(LINLEN),A
	LD	HL,0800H
	LD	(TXTCOL),HL
	LD	HL,(TXTNAM)
	LD	(NAMBAS),HL
	LD	HL,(TXTCGP)
	CP	40+1
	JR	C,J0A08
	LD	HL,1000H
J0A08:	LD	(CGPBAS),HL
	CALL	C0B42			; SETTXT
J0A0E:	CALL	C0953			; change colors
	CALL	C0864			; clear screen in textmode
	CALL	C078A			; initialize characterpatterns
	JP	J0632			; enable screendisplay

;	  Subroutine INIT32
;	     Inputs  ________________________
;	     Outputs ________________________

C0A1A:	LD	A,1
	LD	(OLDSCR),A		; force to textmode reverts to screenmode 1
	CALL	C0B22			; setup for screenmode change
	LD	A,(LINL32)
	LD	(LINLEN),A
	LD	HL,(T32NAM)
	LD	(NAMBAS),HL
	LD	HL,(T32CGP)
	LD	(CGPBAS),HL
	LD	HL,(T32PAT)
	LD	(PATBAS),HL
	LD	HL,(T32ATR)
	LD	(ATRBAS),HL
	CALL	C0B9C			; SETT32
	CALL	C0714			; initialize spriteplanes and spritecolor table
	JR	J0A0E

;	  Subroutine INIGRP
;	     Inputs  ________________________
;	     Outputs ________________________

C0A48:	LD	A,2
	CALL	C0B22			; setup for screenmode change
	LD	HL,(GRPATR)
	CALL	C0A82			; initialize ATRBAS,PATBAS,CGPBAS and NAMBAS
	PUSH	HL
	CALL	C0BD2			; SETGRP
J0A57:	POP	HL
	CALL	C08E3			; set VRAM pointer for writing (active page)
	XOR	A
	LD	B,3
J0A5E:	OUT	(VDP_PORT),A
	INC	A
	JR	NZ,J0A5E
	DJNZ	J0A5E
	CALL	C0895			; clear screen in screenmode 2/4
J0A68:	CALL	C0953			; change colors
	CALL	C0714			; initialize spriteplanes and spritecolor table
	JP	J0632			; enable screendisplay

;	  Subroutine initialize screenmode 4
;	     Inputs  ________________________
;	     Outputs ________________________

C0A71:	LD	A,4
	CALL	C0B22			; setup for screenmode change
	LD	HL,1E00H
	CALL	C0A82			; initialize ATRBAS,PATBAS,CGPBAS and NAMBAS
	PUSH	HL
	CALL	C0BD9			; setup for GRAPHIC 3 mode (screenmode 4)
	JR	J0A57

;	  Subroutine initialize ATRBAS,PATBAS,CGPBAS and NAMBAS
;	     Inputs  ________________________
;	     Outputs ________________________

C0A82:	LD	(ATRBAS),HL
	LD	HL,(GRPPAT)
	LD	(PATBAS),HL
	LD	HL,(GRPCGP)
	LD	(CGPBAS),HL
	LD	HL,(GRPNAM)
	LD	(NAMBAS),HL
	RET

;	  Subroutine INIMLT
;	     Inputs  ________________________
;	     Outputs ________________________

C0A98:	LD	A,3
	CALL	C0B22			; setup for screenmode change
	LD	HL,(MLTPAT)
	LD	(PATBAS),HL
	LD	HL,(MLTATR)
	LD	(ATRBAS),HL
	LD	HL,(MLTCGP)
	LD	(CGPBAS),HL
	LD	HL,(MLTNAM)
	LD	(NAMBAS),HL
	PUSH	HL
	CALL	C0C22			; SETMLT
	POP	HL
	CALL	C08E3			; set VRAM pointer for writing (active page)
	LD	DE,6
J0AC0:	LD	C,4
J0AC2:	LD	A,D
	LD	B,32
J0AC5:	OUT	(VDP_PORT),A
	INC	A
	DJNZ	J0AC5
	DEC	C
	JR	NZ,J0AC2
	LD	D,A
	DEC	E
	JR	NZ,J0AC0
	CALL	C08AD			; clear screen in screenmode 3
	JR	J0A68

;	  Subroutine initialize screenmode 5
;	     Inputs  ________________________
;	     Outputs ________________________

C0AD6:	LD	A,5
	LD	DE,07876H
	CALL	C0B14			; initialize PATBAS,ATRBAS and NAMBAS, setup for screenmode change
	CALL	C0C56			; setup for GRAPHIC 4 mode (screenmode 5)
	JR	J0B08

;	  Subroutine initialize screenmode 6
;	     Inputs  ________________________
;	     Outputs ________________________

C0AE3:	LD	A,6
	LD	DE,07876H
	CALL	C0B14			; initialize PATBAS,ATRBAS and NAMBAS, setup for screenmode change
	CALL	C0C80			; setup for GRAPHIC 5 mode (screenmode 6)
	JR	J0B08

;	  Subroutine initialize screenmode 7
;	     Inputs  ________________________
;	     Outputs ________________________

C0AF0:	LD	A,7
	LD	DE,0F0FAH
	CALL	C0B14			; initialize PATBAS,ATRBAS and NAMBAS, setup for screenmode change
	CALL	C0C97			; setup for GRAPHIC 6 mode (screenmode 7)
	JR	J0B08

;	  Subroutine initialize screenmode 8
;	     Inputs  ________________________
;	     Outputs ________________________

C0AFD:	LD	A,8
	LD	DE,0F0FAH
	CALL	C0B14			; initialize PATBAS,ATRBAS and NAMBAS, setup for screenmode change
	CALL	C0CC7			; setup for GRAPHIC 7 mode (screenmode 8)
J0B08:	CALL	C0953			; change colors
	CALL	C0811
	CALL	C0714			; initialize spriteplanes and spritecolor table
	JP	J0632			; enable screendisplay


;	  Subroutine initialize PATBAS,ATRBAS and NAMBAS, setup for screenmode change
;	     Inputs  ________________________
;	     Outputs ________________________

C0B14:	LD	L,0
	LD	H,D
	LD	(PATBAS),HL
	LD	H,E
	LD	(ATRBAS),HL
	LD	H,L
	LD	(NAMBAS),HL

;	  Subroutine setup for screenmode change
;	     Inputs  ________________________
;	     Outputs ________________________

C0B22:	LD	(SCRMOD),A		; set current screenmode
	XOR	A
	LD	(DPPAGE),A		; display page=0
	LD	(ACPAGE),A		; active page=0
	LD	HL,256
	LD	(ASPCT1),HL
	LD	(ASPCT2),HL		; 1:1 ratio for circle
	CALL	C2980			; wait for vdp command to complete
	CALL	C063C			; disable screendisplay
	LD	B,0
	LD	C,23
	JP	C0647			; write vdp register 23 (reset vertical scroll)

;	  Subroutine SETTXT
;	     Inputs  ________________________
;	     Outputs ________________________

C0B42:	CALL	C0D2D			; set vertical screensize to 192 lines
	LD	A,(LINLEN)
	CP	40+1
	LD	B,00H
	JR	C,J0B50
	LD	B,04H
J0B50:	LD	C,10H
	CALL	C0D3F			; change mode bits
	LD	A,(LINLEN)
	CP	40+1
	LD	A,(DPPAGE)
	JR	NC,J0B72
	ADD	A,A
	LD	HL,(TXTCGP)
	LD	B,0
	CALL	C0CE6			; set pattern generator table address
	ADD	A,A
	LD	HL,(TXTNAM)
	CALL	C0CDA			; set pattern name table address
	JP	J06CA			; update vdp registers from backup

J0B72:	LD	A,(DPPAGE)
	PUSH	AF
	ADD	A,A
	ADD	A,A
	LD	B,00H
	LD	HL,(CGPBAS)
	CALL	C0CE6			; set pattern generator table address
	ADD	A,A
	LD	B,3
	LD	HL,(TXTNAM)
	CALL	C0CDA			; set pattern name table address
	POP	AF
	LD	HL,(TXTCOL)
	LD	E,00H
	SRL	A
	RR	E
	LD	D,A
	LD	B,07H
	CALL	C0CF4			; set color table address
	JP	J06CA			; update vdp registers from backup

;	  Subroutine SETT32
;	     Inputs  ________________________
;	     Outputs ________________________

C0B9C:	CALL	C0D2D			; set vertical screensize to 192 lines
	LD	BC,0000H
	CALL	C0D3F			; change mode bits
	LD	A,(DPPAGE)
	LD	C,A
	LD	B,00H
	LD	HL,(T32CGP)
	ADD	A,A
	ADD	A,A
	ADD	A,A
	CALL	C0CE6			; set pattern generator table address
	LD	HL,(T32PAT)
	CALL	C0D20			; set sprite pattern generator table address
	LD	HL,(T32NAM)
	ADD	A,A
	CALL	C0CDA			; set pattern name table address
	LD	E,B
	LD	D,C
	LD	HL,(T32COL)
	CALL	C0CF4			; set color table address
	SRL	D
	RR	E
	LD	HL,(T32ATR)
	JR	J0C1C			; set sprite attribute table address and update vdp registers from backup

;	  Subroutine SETGRP
;	     Inputs  ________________________
;	     Outputs ________________________

C0BD2:	XOR	A
	PUSH	AF
	LD	BC,0200H
	JR	J0BE0

;	  Subroutine setup for GRAPHIC 3 mode (screenmode 4)
;	     Inputs  ________________________
;	     Outputs ________________________

C0BD9:	LD	A,3
	OR	A
	PUSH	AF
	LD	BC,0400H
J0BE0:	CALL	C0D3F			; change mode bits
	CALL	C0D2D			; set vertical screensize to 192 lines
	LD	A,(DPPAGE)
	LD	C,A
	LD	B,03H	; 3 
	LD	HL,(GRPCGP)
	ADD	A,A
	ADD	A,A
	ADD	A,A
	CALL	C0CE6			; set pattern generator table address
	LD	HL,(GRPPAT)
	CALL	C0D20			; set sprite pattern generator table address
	LD	HL,(GRPNAM)
	ADD	A,A
	LD	B,0
	CALL	C0CDA			; set pattern name table address
	LD	E,B
	LD	D,C
	LD	B,7FH
	LD	HL,(GRPCOL)
	CALL	C0CF4			; set color table address
	SRL	D
	RR	E
	POP	AF
	LD	B,A
	LD	HL,(GRPATR)
	JR	Z,J0C1C			; screenmode 2, set sprite attribute table address and update vdp registers from backup
	LD	HL,(ATRBAS)
J0C1C:	CALL	C0D0B			; set sprite attribute table address
	JP	J06CA			; update vdp registers from backup

;	  Subroutine SETMLT
;	     Inputs  ________________________
;	     Outputs ________________________

C0C22:	CALL	C0D2D			; set vertical screensize to 192 lines
	LD	BC,0008H
	CALL	C0D3F			; change mode bits
	LD	A,(DPPAGE)
	LD	C,A
	LD	B,00H
	LD	HL,(MLTCGP)
	ADD	A,A
	ADD	A,A
	ADD	A,A
	CALL	C0CE6			; set pattern generator table address
	LD	HL,(MLTPAT)
	CALL	C0D20			; set sprite pattern generator table address
	LD	HL,(MLTNAM)
	ADD	A,A
	LD	B,0
	CALL	C0CDA			; set pattern name table address
	LD	E,B
	LD	D,C
	SRL	D
	RR	E
	LD	B,00H
	LD	HL,(MLTATR)
	JR	J0C1C			; set sprite attribute table address and update vdp registers from backup

;	  Subroutine setup for GRAPHIC 4 mode (screenmode 5)
;	     Inputs  ________________________
;	     Outputs ________________________

C0C56:	CALL	C0D36			; set vertical screensize to 212 lines
	LD	BC,0600H
	CALL	C0D3F			; change mode bits
J0C5F:	LD	A,(DPPAGE)
	LD	C,A
	LD	B,00H
	ADD	A,A
	ADD	A,A
	ADD	A,A
	ADD	A,A
	CALL	C0D1D			; set sprite pattern generator table address to PATBAS
	LD	HL,(NAMBAS)
	ADD	A,A
	LD	B,1FH
	CALL	C0CDA			; set pattern name table address
	LD	D,C
J0C76:	LD	E,00H
	LD	B,03H
	CALL	C0D08			; set sprite attribute table address to ATRBAS
	JP	J06CA			; update vdp registers from backup

;	  Subroutine setup for GRAPHIC 5 mode (screenmode 6)
;	     Inputs  ________________________
;	     Outputs ________________________

C0C80:	CALL	C0D36			; set vertical screensize to 212 lines
	LD	BC,0800H
	CALL	C0D3F			; change mode bits
	LD	HL,512
	LD	(ASPCT1),HL
	LD	HL,128
	LD	(ASPCT2),HL		; 2:1 ratio for circle
	JR	J0C5F

;	  Subroutine setup for GRAPHIC 6 mode (screenmode 7)
;	     Inputs  ________________________
;	     Outputs ________________________

C0C97:	CALL	C0D36			; set vertical screensize to 212 lines
	LD	BC,0A00H
	CALL	C0D3F			; change mode bits
	LD	HL,512
	LD	(ASPCT1),HL
	LD	HL,128
	LD	(ASPCT2),HL		; 2:1 ratio for circle
J0CAC:	LD	A,(DPPAGE)
	LD	C,A
	LD	B,00H
	RRCA
	RRCA
	RRCA
	AND	0E0H
	CALL	C0D1D			; set sprite pattern generator table address to PATBAS
	LD	HL,(NAMBAS)
	LD	B,1FH
	CALL	C0CDA			; set pattern name table address
	LD	A,C
	ADD	A,A
	LD	D,A
	JR	J0C76

;	  Subroutine setup for GRAPHIC 7 mode (screenmode 8)
;	     Inputs  ________________________
;	     Outputs ________________________

C0CC7:	CALL	C0D36			; set vertical screensize to 212 lines
	LD	BC,0E00H
	CALL	C0D3F			; change mode bits
	LD	A,(RG2SAV)
	SRL	A
	LD	(RG2SAV),A		; ????
	JR	J0CAC


;	  Subroutine set pattern name table address
;	     Inputs  ________________________
;	     Outputs ________________________

C0CDA:	LD	L,A
	SRL	H
	SRL	H
	ADD	A,H
	OR	B
	LD	(RG2SAV),A
	LD	A,L
	RET


;	  Subroutine set pattern generator table address
;	     Inputs  ________________________
;	     Outputs ________________________

C0CE6:	LD	L,A
	SRL	H
	SRL	H
	SRL	H
	ADD	A,H
	OR	B
	LD	(RG4SAV),A
	LD	A,L
	RET


;	  Subroutine set color table address
;	     Inputs  ________________________
;	     Outputs ________________________

C0CF4:	PUSH	AF
	XOR	A
	ADD	HL,HL
	ADC	A,A
	ADD	HL,HL
	ADC	A,A
	LD	L,H
	LD	H,A
	ADD	HL,DE
	LD	A,L
	OR	B
	LD	(RG3SAV),A
	LD	A,H
	LD	(RG10SA),A
	POP	AF
	RET


;	  Subroutine set sprite attribute table address to ATRBAS
;	     Inputs  ________________________
;	     Outputs ________________________

C0D08:	LD	HL,(ATRBAS)

;	  Subroutine set sprite attribute table address
;	     Inputs  ________________________
;	     Outputs ________________________

C0D0B:	PUSH	AF
	XOR	A
	ADD	HL,HL
	ADC	A,A
	LD	L,H
	LD	H,A
	ADD	HL,DE
	LD	A,L
	OR	B
	LD	(RG5SAV),A
	LD	A,H
	LD	(RG11SA),A
	POP	AF
	RET


;	  Subroutine set sprite pattern generator table address to PATBAS
;	     Inputs  ________________________
;	     Outputs ________________________

C0D1D:	LD	HL,(PATBAS)

;	  Subroutine set sprite pattern generator table address
;	     Inputs  ________________________
;	     Outputs ________________________

C0D20:	LD	L,A
	SRL	H
	SRL	H
	SRL	H
	ADD	A,H
	LD	(RG6SAV),A
	LD	A,L
	RET

;	  Subroutine set vertical screensize to 192 lines
;	     Inputs  ________________________
;	     Outputs ________________________

C0D2D:	LD	A,(RG9SAV)
	AND	7FH
	LD	(RG9SAV),A
	RET


;	  Subroutine set vertical screensize to 212 lines
;	     Inputs  ________________________
;	     Outputs ________________________

C0D36:	LD	A,(RG9SAV)
	OR	80H
	LD	(RG9SAV),A
	RET


;	  Subroutine change mode bits
;	     Inputs  ________________________
;	     Outputs ________________________

C0D3F:	LD	A,(RG0SAV)
	AND	0F1H
	OR	B
	LD	(RG0SAV),A
	LD	A,(RG1SAV)
	AND	0E7H
	OR	C
	LD	(RG1SAV),A
	RET

;	  Subroutine enable functionkey display
;	     Inputs  ________________________
;	     Outputs ________________________

J0D52:	LD	A,0FFH
	CALL	C061D			; set functionkey display flag and check if in textmode
	RET	NC			; not in textmode, quit
	PUSH	HL
	LD	A,(CSRY)
	LD	HL,CRTCNT
	CP	(HL)			; cursor on last line ?
	LD	A,10
	JR	NZ,J0D65
	RST	18H			; yep, give linefeed to create empty line
J0D65:	LD	A,(NEWKEY+6)
	RRCA				; SHIFT key pressed ?
	LD	HL,FNKSTR+0*16
	LD	A,1
	JR	C,J0D74			; nope, display F1-F5
	LD	HL,FNKSTR+5*16
	XOR	A			; yep, display F6-F10
J0D74:	LD	(FNKSWI),A
	CALL	C0DE2			; empty line in LINWRK
	LD	C,5			; 5 functionkeys
	LD	A,(LINLEN)
	CP	40+1			; screenwidth >40 ?
	JR	NC,J0DA2		; yep, do functionkey display in two parts
	CALL	C0DF0			; calculate size for one functionkey
	JR	C,J0D9A			; no space, display nothing
J0D88:	PUSH	BC
	LD	C,0
J0D8B:	CALL	C0DFE			; put char in LINWRK if printable
	DJNZ	J0D8B
	LD	A,16
	SUB	C
	LD	C,A
	ADD	HL,BC			; to next functionkey definition
	INC	DE			; space between functionkeys
	POP	BC
	DEC	C
	JR	NZ,J0D88		; next functionkey
J0D9A:	LD	HL,(CRTCNT)
	CALL	C0ED7			; write line from LINWRK to VRAM
	POP	HL
	RET

J0DA2:	PUSH	AF
	CALL	C0DF0			; calculate size for one functionkey
	POP	AF
	SUB	40
J0DA9:	PUSH	BC
	LD	C,0
J0DAC:	EX	AF,AF'
	CALL	C0DFE			; put char in LINWRK if printable
	EX	AF,AF'
	DEC	A			; first part of line full ?
	CALL	Z,C0DD2			; yep, write line from LINWRK to VRAM (first part), clear LINWRK
	DJNZ	J0DAC
	DEC	A			; first part of line full ?
	JR	NZ,J0DBE
	CALL	C0DD2			; yep, write line from LINWRK to VRAM (first part), clear LINWRK
	DEC	DE
J0DBE:	EX	AF,AF'
	LD	A,16
	SUB	C
	LD	C,A
	ADD	HL,BC			; to next functionkey definition
	EX	AF,AF'
	INC	DE			; space between functionkeys
	POP	BC
	DEC	C
	JR	NZ,J0DA9		; next functionkey
	LD	HL,(CRTCNT)
	CALL	C0EF2			; write line from LINWRK to VRAM, second part
	POP	HL
	RET

;	  Subroutine write line from LINWRK to VRAM (first part), clear LINWRK
;	     Inputs  ________________________
;	     Outputs ________________________

C0DD2:	PUSH	BC
	PUSH	HL
	LD	HL,(CRTCNT)
	CALL	C0ED8			; write line from LINWRK to VRAM, first part
	CALL	C0DE2			; empty line in LINWRK
	POP	HL
	POP	BC
	LD	A,0FFH
	RET

;	  Subroutine empty line in LINWRK
;	     Inputs  ________________________
;	     Outputs ________________________

C0DE2:	LD	DE,LINWRK
	PUSH	DE
	LD	B,40
	LD	A," "
J0DEA:	LD	(DE),A
	INC	DE
	DJNZ	J0DEA
	POP	DE
	RET


;	  Subroutine calculate size for one functionkey
;	     Inputs  ________________________
;	     Outputs ________________________

C0DF0:	SUB	4
	RET	C
	LD	B,-1
J0DF5:	INC	B
	SUB	5
	JR	NC,J0DF5
	LD	A,B
	SUB	1
	RET

;	  Subroutine put char in LINWRK if printable
;	     Inputs  ________________________
;	     Outputs ________________________

C0DFE:	LD	A,(HL)
	INC	HL
	INC	C
	CALL	C12A5			; check if graphical header char
	JR	NC,C0DFE		; graphic header char, next char
	JR	NZ,J0E0C		; graphic char, ok
	CP	20H			; control char ?
	JR	C,J0E0D			; yep,
J0E0C:	LD	(DE),A
J0E0D:	INC	DE
	RET

;	  Subroutine delete line from screen (DELLNO)
;	     Inputs  ________________________
;	     Outputs ________________________

J0E0F:	CALL	C0F00			; get number of screen lines
	SUB	L			; linenumber valid ?
	RET	C			; nope, quit
	JP	Z,J0EA9			; last line, clear line and quit
	PUSH	HL
	PUSH	AF
	LD	C,A
	LD	B,0
	CALL	C0F0A			; get LINTTB entry of line
	LD	L,E
	LD	H,D
	INC	HL
	LDIR
	LD	HL,FSTPOS
	DEC	(HL)
	POP	AF
	POP	HL
	PUSH	AF
	LD	A,(LINLEN)
	CP	40+1			; screenwidth >40 ?
	JR	NC,J0E43		; yep, do it in two parts
	POP	AF
J0E33:	PUSH	AF
	INC	L
	CALL	C0EAE			; read line from VRAM to LINWRK
	DEC	L
	CALL	C0ED7			; write line from LINWRK to VRAM
	INC	L
	POP	AF
	DEC	A
	JR	NZ,J0E33		; next line
	JR	J0E5A			; clear line and quit

J0E43:	POP	AF
J0E44:	PUSH	AF
	INC	L
	CALL	C0EAF			; read line from VRAM to LINWRK, first part
	DEC	L
	CALL	C0ED8			; write line from LINWRK to VRAM, first part
	INC	L
	CALL	C0EC8			; read line from VRAM to LINWRK, second part
	DEC	L
	CALL	C0EF2			; write line from LINWRK to VRAM, second part
	INC	L
	POP	AF
	DEC	A
	JR	NZ,J0E44		; next line
J0E5A:	JP	J0EA9			; clear line and quit

;	  Subroutine insert line to screen (INSLNO)
;	     Inputs  ________________________
;	     Outputs ________________________

J0E5D:	CALL	C0F00			; get number of screen lines
	LD	H,A
	SUB	L			; linenumber valid ?
	RET	C			; nope, quit
	JP	Z,J0EA9			; last line, clear line and quit
	LD	L,H
	PUSH	HL
	PUSH	AF
	LD	C,A
	LD	B,0
	CALL	C0F0A			; get LINTTB entry of line
	LD	L,E
	LD	H,D
	PUSH	HL
	DEC	HL
	LDDR
	POP	HL
	LD	(HL),H
	POP	AF
	POP	HL
	PUSH	AF
	LD	A,(LINLEN)
	CP	40+1			; screenwidth >40 ?
	JR	NC,J0E92		; yep, do it in two parts
	POP	AF
J0E82:	PUSH	AF
	DEC	L
	CALL	C0EAE			; read line from VRAM to LINWRK
	INC	L
	CALL	C0ED7			; write line from LINWRK to VRAM
	DEC	L
	POP	AF
	DEC	A
	JR	NZ,J0E82		; next line
	JR	J0EA9			; clear line and quit

J0E92:	POP	AF
J0E93:	PUSH	AF
	DEC	L
	CALL	C0EAF			; read line from VRAM to LINWRK, first part
	INC	L
	CALL	C0ED8			; write line from LINWRK to VRAM, first part
	DEC	L
	CALL	C0EC8			; read line from VRAM to LINWRK, second part
	INC	L
	CALL	C0EF2			; write line from LINWRK to VRAM, second part
	DEC	L
	POP	AF
	DEC	A
	JR	NZ,J0E93		; next line
J0EA9:	LD	H,1			; column 1
	JP	J0596			; clear to end of line

;	  Subroutine read line from VRAM to LINWRK
;	     Inputs  L = line
;	     Outputs ________________________

C0EAE:	DEFB	0F6H

;	  Subroutine read line from VRAM to LINWRK, first part
;	     Inputs  ________________________
;	     Outputs ________________________

C0EAF:	XOR	A
	PUSH	HL
	PUSH	AF
	LD	H,1			; column = 1
	CALL	C0F35			; convert coordinate to VRAM address
	POP	AF
	LD	A,(LINLEN)
	JR	NZ,J0EBF
	SUB	40
J0EBF:	LD	B,A
J0EC0:	LD	DE,LINWRK
	CALL	C0F22			; read string from VRAM
	POP	HL
	RET

;	  Subroutine read line from VRAM to LINWRK, second part
;	     Inputs  ________________________
;	     Outputs ________________________

C0EC8:	PUSH	HL
	LD	A,(LINLEN)
	SUB	40
	INC	A
	LD	H,A			; column
	CALL	C0F35			; convert coordinate to VRAM address
	LD	B,40
	JR	J0EC0			; read string from VRAM in LINWRK


;	  Subroutine write line from LINWRK to VRAM
;	     Inputs  ________________________
;	     Outputs ________________________

C0ED7:	DEFB	0F6H

;	  Subroutine write line from LINWRK to VRAM, first part
;	     Inputs  ________________________
;	     Outputs ________________________

C0ED8:	XOR	A
	PUSH	HL
	PUSH	AF
	LD	H,1			; column = 1
	CALL	C0F35			; convert coordinate to VRAM address
	POP	AF
	LD	A,(LINLEN)
	JR	NZ,J0EE8
	SUB	40
J0EE8:	LD	B,A
J0EE9:	EX	DE,HL
	LD	HL,LINWRK
	CALL	C0F2B			; write string to VRAM
	POP	HL
	RET

;	  Subroutine write line from LINWRK to VRAM, second part
;	     Inputs  ________________________
;	     Outputs ________________________

C0EF2:	PUSH	HL
	LD	A,(LINLEN)
	SUB	40-1
	LD	H,A			; column
	CALL	C0F35			; convert coordinate to VRAM address
	LD	B,40
	JR	J0EE9			; write string in LINWRK to VRAM


;	  Subroutine get number of screen lines
;	     Inputs  ________________________
;	     Outputs ________________________

C0F00:	LD	A,(CNSDFG)
	PUSH	HL
	LD	HL,CRTCNT
	ADD	A,(HL)
	POP	HL
	RET


;	  Subroutine get LINTTB entry of line
;	     Inputs  ________________________
;	     Outputs ________________________

C0F0A:	PUSH	HL
	LD	DE,LINTTB-1
	LD	H,0
	ADD	HL,DE
	LD	A,(HL)
	EX	DE,HL
	POP	HL
	AND	A
	RET

;	  Subroutine put char to screen (PUTVRM)
;	     Inputs  ________________________
;	     Outputs ________________________

J0F16:	PUSH	HL
	CALL	C0F35			; convert coordinate to VRAM address
	CALL	C08E3			; set VRAM pointer for writing (active page)
	LD	A,C
	OUT	(VDP_PORT),A
	POP	HL
	RET


;	  Subroutine read string from VRAM
;	     Inputs  ________________________
;	     Outputs ________________________

C0F22:	CALL	C08F1			; set VRAM pointer for reading (active page)
	EX	DE,HL
	LD	C,VDP_PORT
	INIR
	RET


;	  Subroutine write string to VRAM
;	     Inputs  ________________________
;	     Outputs ________________________

C0F2B:	EX	DE,HL
	CALL	C08E3			; set VRAM pointer for writing (active page)
	EX	DE,HL
	LD	C,VDP_PORT
	OTIR
	RET


;	  Subroutine convert coordinate to VRAM address
;	     Inputs  L = line, H = col
;	     Outputs ________________________

C0F35:	PUSH	BC
	DEC	H
	DEC	L
	LD	E,H
	LD	H,0
	LD	D,H
	ADD	HL,HL
	ADD	HL,HL
	ADD	HL,HL
	LD	C,L
	LD	B,H			; *8
	ADD	HL,HL
	ADD	HL,HL			; *32
	LD	A,(SCRMOD)
	AND	A			; screenmode 0 ?
	LD	A,(LINLEN)
	JR	Z,J0F50			; yep,
	SUB	32+2
	JR	J0F5D

J0F50:	CP	40+1			; 40 colum mode ?
	JR	C,J0F5A			; yep, *40
	ADD	HL,BC
	ADD	HL,HL			; *80
	SUB	80+2
	JR	J0F5D

J0F5A:	ADD	HL,BC
	SUB	40+2
J0F5D:	ADD	HL,DE
	CPL
	AND	A
	RRA
	LD	E,A
	ADD	HL,DE
	EX	DE,HL
	LD	HL,(NAMBAS)
	ADD	HL,DE
	POP	BC
	RET


;	  Subroutine get pallette values of color (GETPLT)
;	     Inputs  ________________________
;	     Outputs ________________________

C0F6A:	PUSH	HL
	LD	D,A			; color
	CALL	C0FF9			; get pallette table address in VRAM
	CALL	C08F1			; set VRAM pointer for reading (active page)
	EX	(SP),HL
	EX	(SP),HL			; wait
	IN	A,(VDP_PORT)
	LD	B,A
	NOP
	NOP
	IN	A,(VDP_PORT)
	LD	C,A
	POP	HL
	RET

;	  Subroutine restore pallette (RSTPLT)
;	     Inputs  ________________________
;	     Outputs ________________________

J0F7E:	PUSH	HL
	LD	D,0			; color 0
	CALL	C0FF9			; get pallette table address in VRAM
	CALL	C08F1			; set VRAM pointer for reading (active page)
	LD	B,D
	LD	C,16
	CALL	C0647			; write vdp register 16
	LD	B,16			; 16 colors
J0F8F:	IN	A,(VDP_PORT)			; read VRAM
	NOP				; wait
	OUT	(VDP_PORT+2),A			; write pallette register
	NOP				; wait
	IN	A,(VDP_PORT)			; read VRAM
	NOP				; wait
	OUT	(VDP_PORT+2),A			; write pallette register
	NOP				; wait
	DJNZ	J0F8F			; next color
	POP	HL
	RET

;	  Subroutine initialize pallette (INIPLT)
;	     Inputs  ________________________
;	     Outputs ________________________

J0F9F:	PUSH	HL
	LD	HL,I0FB3
	LD	B,16			; 16 colors
	LD	D,0			; starting with color 0
J0FA7:	LD	A,(HL)
	INC	HL
	LD	E,(HL)
	INC	HL
	CALL	C0FD3			; set pallette values of color (SETPLT)
	INC	D
	DJNZ	J0FA7			; next color
	POP	HL
	RET

I0FB3:	DEFB	00H,00H
	DEFB	00H,00H
	DEFB	11H,06H
	DEFB	33H,07H
	DEFB	17H,01H
	DEFB	27H,03H
	DEFB	51H,01H
	DEFB	27H,06H
	DEFB	71H,01H
	DEFB	73H,03H
	DEFB	61H,06H
	DEFB	64H,06H
	DEFB	11H,04H
	DEFB	65H,02H
	DEFB	55H,05H
	DEFB	77H,07H

;	  Subroutine set pallette values of color (SETPLT)
;	     Inputs  ________________________
;	     Outputs ________________________

C0FD3:	PUSH	BC
	PUSH	HL
	LD	B,D
	LD	C,16
	PUSH	AF
	CALL	C0647			; write vdp register 16
	POP	AF
	LD	C,VDP_PORT+2
	OUT	(C),A
	PUSH	AF
	POP	AF
	OUT	(C),E
	CALL	C0FF9
	PUSH	AF
	CALL	C08E3			; set VRAM pointer for writing (active page)
	EX	(SP),HL
	EX	(SP),HL			; wait
	POP	AF
	OUT	(VDP_PORT),A			; write VRAM
	PUSH	AF
	POP	AF			; wait
	LD	A,E
	OUT	(VDP_PORT),A			; write VRAM
	POP	HL
	POP	BC
	RET

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C0FF9:	PUSH	AF
	LD	A,(SCRMOD)
	LD	HL,I1020
	PUSH	BC
	INC	A
	LD	C,A
	LD	B,0			; screenmode, 1 based
	DEC	A			; screenmode 0 ?
	JR	NZ,J1010		; nope,
	LD	A,(LINLEN)
	CP	40+1			; 40 column mode ?
	JR	NC,J1010		; nope,
	DEC	C			; yep, use first entry of table
J1010:	ADD	HL,BC
	ADD	HL,BC
	POP	BC
	LD	A,(HL)
	INC	HL
	LD	H,(HL)
	LD	L,A
	PUSH	DE
	LD	E,D
	LD	D,0
	ADD	HL,DE
	ADD	HL,DE
	POP	DE
	POP	AF
	RET

I1020:	DEFW	00400H			; screenmode 0, 40 cols
	DEFW	00F00H			; screenmode 0, 80 cols
	DEFW	02020H			; screenmode 1
	DEFW	01B80H			; screenmode 2
	DEFW	02020H			; screenmode 3
	DEFW	01B80H			; screenmode 4
	DEFW	07680H			; screenmode 5
	DEFW	07680H			; screenmode 6
	DEFW	0FA80H			; screenmode 7
	DEFW	0FA80H			; screenmode 8


;	  Subroutine wait for VR to pass
;	     Inputs  ________________________
;	     Outputs ________________________

C1034:	LD	A,2
	CALL	C298B			; read vdp status register 2
	AND	40H			; in VR ?
	JR	NZ,C1034		; nope, wait
J103D:	LD	A,2
	CALL	C298B			; read vdp status register 2
	AND	40H			; VR passed ?
	JR	Z,J103D			; nope, wait
	RET

J1047:	POP	AF
	RET

J1049:	PUSH	AF
	LD	A,B
	CP	21H
	JR	C,J1047
	CP	28H
	JR	C,J105B
	CP	30H
	JR	C,J1047
	CP	50H
	JR	NC,J1047
J105B:	LD	A,C
	CP	21H
	JR	C,J1047
	CP	7FH
	JR	NC,J1047
	POP	AF
	PUSH	HL
	PUSH	DE
	PUSH	BC
	PUSH	AF
	LD	A,C
	SUB	32
	LD	E,A
	LD	D,00H
	LD	A,B
	SUB	32
	LD	L,A
	LD	H,D
	LD	C,L
	LD	B,H
	ADD	HL,HL
	ADD	HL,BC
	ADD	HL,HL
	ADD	HL,HL
	ADD	HL,HL
	ADD	HL,HL
	ADD	HL,HL
	ADD	HL,DE
	CP	10H
	JR	C,J1084
	DEC	H
	DEC	H
J1084:	LD	A,L
	OUT	(KANJI1_PORT),A
	ADD	HL,HL
	ADD	HL,HL
	LD	A,H
	OUT	(KANJI1_PORT+1),A
	LD	BC,$2000 | (KANJI1_PORT+1)
	LD	HL,LINWRK
	INIR
	POP	AF
	PUSH	AF
	AND	A
	LD	C,2
	JR	Z,J10C5
	LD	HL,LINWRK
	LD	E,L
	LD	D,H
	DEC	A
	JR	Z,J10A4
	INC	HL
J10A4:	LD	C,2
J10A6:	LD	B,8
J10A8:	LD	A,(HL)
	LD	(DE),A
	INC	HL
	INC	HL
	INC	DE
	LD	A,B
	CP	5
	JR	NZ,J10B8
	PUSH	BC
	LD	BC,8
	ADD	HL,BC
	POP	BC
J10B8:	DJNZ	J10A8
	PUSH	BC
	LD	BC,-16
	ADD	HL,BC
	POP	BC
	DEC	C
	JR	NZ,J10A6
	LD	C,1
J10C5:	LD	HL,(GRPACX)
	PUSH	HL
	LD	HL,(GRPACY)
	PUSH	HL
	LD	DE,LINWRK
J10D0:	LD	HL,(GRPACY)
	PUSH	HL
	LD	HL,(GRPACX)
	PUSH	HL
	LD	B,2
J10DA:	PUSH	BC
	EX	DE,HL
	LD	DE,PATWRK
	LD	BC,8
	LDIR
	PUSH	HL
	CALL	C112D
	POP	DE
	POP	BC
	DJNZ	J10DA
	POP	HL
	LD	(GRPACX),HL
	POP	HL
	PUSH	BC
	LD	BC,8
	ADD	HL,BC
	POP	BC
	LD	(GRPACY),HL
	DEC	C
	JR	NZ,J10D0
	POP	HL
	LD	(GRPACY),HL
	POP	HL
	LD	BC,16
	ADD	HL,BC
	LD	(GRPACX),HL
I1109:	POP	AF
	POP	BC
	POP	DE
	POP	HL
	RET

;	  Subroutine GRPPRT
;	     Inputs  ________________________
;	     Outputs ________________________

C110E:	PUSH	HL
	PUSH	DE
	PUSH	BC
	PUSH	AF
	LD	HL,I1109
	PUSH	HL
	CALL	C12A5			; check if graphical header char
	RET	NC			; graphic header char, quit
	JR	NZ,J1124		; graphic char, go
	CP	0DH
	JP	Z,J1210			; CR,
	CP	20H
	RET	C			; other control char, quit
J1124:	CALL	C07D9			; copy characterpattern to PATWRK
	LD	A,(FORCLR)
	LD	(ATRBYT),A

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C112D:
	LD	HL,(GRPACY)
	EX	DE,HL
	LD	BC,(GRPACX)
	CALL	C12C4
	RET	NC
	CALL	C1342
	LD	DE,PATWRK
	CALL	C2980			; wait for vdp command to complete
	PUSH	DE
	XOR	A
	CALL	C29DB			; write ARG register
	CALL	C1376
	LD	E,A
	CALL	C29A5			; write DX,DY registers
	LD	A,E
	SUB	0CDH
	LD	L,08H	; 8 
	JR	C,J115A
	LD	L,A
	LD	A,07H	; 7 
	SUB	L
	LD	L,A
J115A:	LD	B,L
	LD	DE,8
	LD	H,D
	CALL	C29C4			; write NX,NY registers
	POP	DE
	LD	A,(ATRBYT)
	LD	L,A
	LD	A,(BAKCLR)
	LD	H,A
	LD	A,(DE)
	ADD	A,A
	LD	C,A
	LD	A,H
	JR	NC,J1172
	LD	A,L
J1172:
	CALL	C29EB			; write colorregister
	LD	A,(LOGOPR)
	AND	0FH
	OR	0B0H			; LMMC (logical move) command
	CALL	C29E3			; execute vdp command
	DI
	LD	A,080H+44
	OUT	(VDP_PORT+1),A
	LD	A,080H+17
	OUT	(VDP_PORT+1),A			; set indirect vdp register pointer to 44, non-autoincrement
	LD	A,C
	LD	C,VDP_PORT+3
	DI
	JR	J11A3

J118E:	DI
	LD	A,080H+44
	OUT	(VDP_PORT+1),A
	LD	A,080H+17
	OUT	(VDP_PORT+1),A			; set indirect vdp register pointer to 44, non-autoincrement
	INC	DE
	LD	A,(DE)
	RLCA
	JR	NC,J11A1
	OUT	(C),L
	JP	J11A3

J11A1:	OUT	(C),H
J11A3:	RLCA
	JR	NC,J11AB
	OUT	(C),L
	JP	J11AD

J11AB:	OUT	(C),H
J11AD:	RLCA
	JR	NC,J11B5
	OUT	(C),L
	JP	J11B7

J11B5:	OUT	(C),H
J11B7:	RLCA
	JR	NC,J11BF
	OUT	(C),L
	JP	J11C1

J11BF:	OUT	(C),H
J11C1:	RLCA
	JR	NC,J11C9
	OUT	(C),L
	JP	J11CB

J11C9:	OUT	(C),H
J11CB:	RLCA
	JR	NC,J11D3
	OUT	(C),L
	JP	J11D5

J11D3:	OUT	(C),H
J11D5:	RLCA
	JR	NC,J11DD
	OUT	(C),L
	JP	J11DF

J11DD:	OUT	(C),H
J11DF:	RLCA
	JR	NC,J11E7
	OUT	(C),L
	JP	J11E9

J11E7:	OUT	(C),H
J11E9:	EI
	DJNZ	J118E
	LD	A,(SCRMOD)
	AND	0FEH
	CP	06H	; 6 
	JR	Z,J1200
	LD	A,(GRPACX)
	ADD	A,08H	; 8 
	JR	C,J1210
	LD	(GRPACX),A
	RET

J1200:	LD	HL,(GRPACX)
	LD	BC,8
	ADD	HL,BC
	LD	A,H
	AND	0FEH
	JR	NZ,J1210
	LD	(GRPACX),HL
	RET

J1210:	LD	HL,0
	LD	(GRPACX),HL
	LD	A,(GRPACY)
	ADD	A,08H	; 8 
	CP	212
	JR	C,J1220
	XOR	A
J1220:	LD	(GRPACY),A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1224:	PUSH	HL
	PUSH	DE
	PUSH	BC
	PUSH	AF
	LD	HL,I1109
	PUSH	HL
	CALL	C12A5			; check if graphic header char
	RET	NC			; graphic header char, quit
	JR	NZ,J123A		; graphic char, ok
	CP	07H
	JP	Z,J1A62			; BELL,
	CP	20H
	RET	C			; other control char, quit
J123A:	CALL	C07D9			; copy characterpattern to PATWRK
	LD	DE,PATWRK
	PUSH	DE
	CALL	C2980			; wait for vdp command to complete
	XOR	A
	CALL	C29DB			; write ARG register
	CALL	C1376
	CALL	C29A5			; write DX,DY registers
	LD	HL,8
	LD	DE,16
	CALL	C29C4			; write NX,NY registers
	POP	DE
	LD	HL,0103H
	LD	A,(DE)
	ADD	A,A
	LD	C,A
	LD	A,H
	JR	NC,J1262
	LD	A,L
J1262:	PUSH	AF
	CALL	C29EB			; write colorregister
	LD	A,0B0H			; LMMC (logical move) command
	CALL	C29E3			; execute vdp command
	DI
	LD	A,080H+44
	OUT	(VDP_PORT+1),A
	LD	A,080H+17
	OUT	(VDP_PORT+1),A			; set indirect vdp register pointer to 44, non-autoincrement
	POP	AF
	OUT	(VDP_PORT+3),A
	LD	A,C
	LD	C,VDP_PORT+3
	LD	B,08H	; 8 
	PUSH	BC
	DEC	B
	JR	J1285

J1280:	INC	DE
	LD	A,(DE)
	PUSH	BC
	LD	B,08H	; 8 
J1285:	RLCA
	JR	NC,J1290
	OUT	(C),L
	PUSH	AF
	POP	AF
	OUT	(C),L
	JR	J1296

J1290:	OUT	(C),H
	PUSH	AF
	POP	AF
	OUT	(C),H
J1296:	DJNZ	J1285
	POP	BC
	DJNZ	J1280
	LD	HL,(CLOC)
	LD	BC,16
	ADD	HL,BC
	JP	J1372


;	  Subroutine check if graphical header char
;	     Inputs  ________________________
;	     Outputs ________________________

C12A5:	PUSH	HL
	PUSH	AF
	LD	HL,GRPHED
	XOR	A
	CP	(HL)
	LD	(HL),A
	JR	Z,J12BC
	POP	AF
	SUB	40H
	CP	20H
	JR	C,J12BA			; graphic char, Reset Zx, Set Cx
	ADD	A,40H
J12B8:	CP	A			; Set Zx
	SCF				; Set Cx
J12BA:	POP	HL
	RET

J12BC:	POP	AF
	CP	01H
	JR	NZ,J12B8
	LD	(HL),A
	POP	HL			; Set Zx, Reset Cx
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C12C4:	CALL	C062C			; new style screenmode ?
	JP	NC,J1305		; yep,
	PUSH	HL
	PUSH	BC
	LD	B,01H	; 1 
	EX	DE,HL
	LD	A,H
	ADD	A,A
	JR	NC,J12D8
	LD	HL,0
	JR	J12E0

J12D8:	LD	DE,192
	RST	20H
	JR	C,J12E2
	EX	DE,HL
	DEC	HL
J12E0:	LD	B,00H
J12E2:	EX	(SP),HL
	LD	A,H
	ADD	A,A
	JR	NC,J12EC
	LD	HL,0
	JR	J12F4

J12EC:	LD	DE,256
	RST	20H
	JR	C,J12F6
	EX	DE,HL
	DEC	HL
J12F4:	LD	B,00H
J12F6:	POP	DE
	SRL	L
	SRL	L
	SRL	E
	SRL	E
	LD	A,B
	RRCA
	LD	C,L
	LD	B,H
	POP	HL
	RET

J1305:	PUSH	HL
	PUSH	BC
	LD	B,01H	; 1 
	EX	DE,HL
	LD	A,H
	ADD	A,A
	JR	NC,J1313
	LD	HL,0
	JR	J131B

J1313:	LD	DE,212
	RST	20H
	JR	C,J131D
	EX	DE,HL
	DEC	HL
J131B:	LD	B,00H
J131D:	EX	(SP),HL
	LD	A,H
	ADD	A,A
	JR	NC,J1327
	LD	HL,0
	JR	J1339

J1327:	LD	A,(SCRMOD)
	AND	02H	; 2 
	LD	DE,512
	JR	NZ,J1334
	LD	DE,256
J1334:	RST	20H
	JR	C,J133B
	EX	DE,HL
	DEC	HL
J1339:	LD	B,00H
J133B:	LD	A,B
	RRCA
	LD	B,H
	LD	C,L
	POP	DE
	POP	HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1342:	CALL	C062C			; new style screenmode ?
	JR	C,J134F			; nope,
	LD	H,B
	LD	L,C
	LD	A,E
	LD	(CMASK),A
	JR	J1372

J134F:	PUSH	BC
	LD	A,C
	RRCA
	LD	A,0F0H
	JR	NC,J1358
	LD	A,0FH
J1358:	LD	(CMASK),A
	LD	A,C
	ADD	A,A
	ADD	A,A
	AND	0F8H
	LD	C,A
	LD	A,E
	AND	07H
	OR	C
	LD	C,A
	LD	A,E
	RRCA
	RRCA
	RRCA
	AND	07H
	LD	B,A
	LD	HL,(MLTCGP)
	ADD	HL,BC
	POP	BC
J1372:	LD	(CLOC),HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1376:	LD	A,(CMASK)
	LD	HL,(CLOC)
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C137D:	LD	(CMASK),A
	LD	(CLOC),HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1384:	CALL	C062C			; new style screenmode ?
	JP	NC,J13A1		; yep,
	PUSH	BC
	PUSH	HL
	CALL	C1376
	LD	B,A
	CALL	C08D3			; read VRAM limit 16 Kb (active page)
	INC	B
	DEC	B
	JP	P,J139C
	RRCA
	RRCA
	RRCA
	RRCA
J139C:	AND	0FH
	POP	HL
	POP	BC
	RET

J13A1:	PUSH	HL
	LD	HL,(CLOC)
	LD	A,(CMASK)
	CALL	C2949
	POP	HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C13AD:	CALL	C1E47
	RET	C
	LD	(ATRBYT),A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C13B5:	PUSH	BC
	PUSH	DE
	PUSH	HL
	CALL	C062C			; new style screenmode ?
	LD	A,(CMASK)
	LD	HL,(CLOC)
	JR	C,J13CA			; nope,
	CALL	C2961
	POP	HL
	POP	DE
	POP	BC
	RET

J13CA:	LD	B,A
	CALL	C08D6			; read VRAM (active page)
	LD	C,A
	LD	A,B
	CPL
	AND	C
	LD	C,A
	LD	A,(ATRBYT)
	INC	B
	DEC	B
	JP	P,J13DF
	ADD	A,A
	ADD	A,A
	ADD	A,A
	ADD	A,A
J13DF:	OR	C
	CALL	C08CB			; write VRAM (active page)
	POP	HL
	POP	DE
	POP	BC
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C13E7:	PUSH	HL
	CALL	C062C			; new style screenmode ?
	CALL	C1376
	JP	C,J1466			; nope,
	DEC	HL
	LD	A,H
	OR	A
	JP	M,J1471
	LD	(CLOC),HL
	POP	HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C13FC:	PUSH	HL
	CALL	C062C			; new style screenmode ?
	CALL	C1376
	JP	C,J1474			; nope,
	LD	A,(CMASK)
	CP	212-1
	JR	NC,J1471
	INC	A
	LD	(CMASK),A
	POP	HL
	AND	A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1414:	CALL	C062C			; new style screenmode ?
	JP	C,J14AE			; nope,
	LD	A,(CMASK)
	OR	A
	SCF
	RET	Z
	DEC	A
	LD	(CMASK),A
	OR	A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1426:	PUSH	HL
	CALL	C1376
	AND	A
	LD	A,0FH	; 15 
	JP	M,J1460
	LD	A,L
	AND	0F8H
	CP	0F8H
	JR	NZ,J1443
	JR	J1471

J1439:	PUSH	HL
	CALL	C1376
	AND	A
	LD	A,0FH	; 15 
	JP	M,J1460
J1443:	PUSH	DE
	LD	DE,8
	LD	A,0F0H
	JR	J145B

J144B:	PUSH	HL
	CALL	C1376
	AND	A
	LD	A,0F0H
	JP	P,J1460
J1455:	PUSH	DE
	LD	DE,-8
	LD	A,0FH	; 15 
J145B:	ADD	HL,DE
	LD	(CLOC),HL
	POP	DE
J1460:	LD	(CMASK),A
	AND	A
	POP	HL
	RET

J1466:	AND	A
	LD	A,0F0H
	JP	P,J1460
	LD	A,L
	AND	0F8H
	JR	NZ,J1455
J1471:	SCF
	POP	HL
	RET

J1474:	PUSH	DE
	PUSH	HL
	LD	HL,(MLTCGP)
	LD	DE,0500H
	ADD	HL,DE
	EX	DE,HL
	POP	HL
	RST	20H
	JR	C,J148E
	LD	A,L
	INC	A
	AND	07H	; 7 
	JR	NZ,J148E
	SCF
	POP	DE
	POP	HL
	RET

J148C:	PUSH	HL
	PUSH	DE
J148E:	CALL	C1376
	INC	HL
	LD	A,L
	LD	DE,00F8H
	JR	J14A2

J1498:	PUSH	HL
	PUSH	DE
J149A:	CALL	C1376
	LD	A,L
	DEC	HL
	LD	DE,0FF08H
J14A2:	AND	07H	; 7 
	JR	NZ,J14A7
	ADD	HL,DE
J14A7:	LD	(CLOC),HL
	AND	A
	POP	DE
	POP	HL
	RET

J14AE:	PUSH	HL
	PUSH	DE
	LD	HL,(MLTCGP)
	LD	DE,256
	ADD	HL,DE
	EX	DE,HL
	LD	HL,(CLOC)
	RST	20H
	JR	NC,J149A
	LD	A,L
	AND	07H	; 7 
	JR	NZ,J149A
	SCF
	POP	DE
	POP	HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C14C7:	AND	A
	PUSH	AF
	LD	A,(SCRMOD)
	CP	8			; screenmode 8 ?
	JR	Z,J14D8			; yep, use all 256 colors
	POP	AF
	CP	16
	CCF
J14D4:	LD	(BDRATR),A
	RET

J14D8:	POP	AF
	JR	J14D4


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C14DB:	LD	HL,0
	LD	C,L
	CALL	C062C			; new style screenmode ?
	JP	C,J1566			; nope,
	PUSH	HL
	PUSH	BC
	PUSH	DE
	LD	HL,(CLOC)
	LD	A,(CMASK)
	LD	E,A
	LD	D,00H
	LD	A,(BDRATR)
	CALL	C28E0
	JR	C,J14FF
	POP	DE
J14FA:	POP	BC
	POP	HL
	LD	D,H
	LD	E,H
	RET

J14FF:	POP	DE
	PUSH	HL
	LD	HL,(CLOC)
	ADD	HL,DE
	POP	DE
	OR	A
	SBC	HL,DE
	JR	C,J14FA
	JR	Z,J14FA
	POP	BC
	POP	BC
	PUSH	HL
	EX	DE,HL
	LD	(CSAVEA),HL
	LD	A,(CMASK)
	LD	(CSAVEM),A
	LD	E,A
	LD	D,00H
	LD	A,(BDRATR)
	CALL	C28E4
	PUSH	AF
	JR	C,J1533
	LD	A,(SCRMOD)
	AND	02H	; 2 
	LD	HL,01FFH
	JR	NZ,J1533
	LD	HL,00FFH
J1533:	LD	(CLOC),HL
	POP	AF
	JR	C,J153A
	INC	HL
J153A:	PUSH	HL
	EX	DE,HL
	LD	HL,(CSAVEA)
	EX	DE,HL
	OR	A
	SBC	HL,DE
	EX	(SP),HL
	PUSH	HL
	EX	DE,HL
	LD	A,(CSAVEM)
	LD	E,A
	LD	D,00H
	LD	A,(ATRBYT)
	CALL	C28E0
	POP	DE
	JR	NC,J1561
	RST	20H
	JR	NC,J1561
	POP	DE
	LD	HL,(CSAVEA)
	CALL	C15DB
	POP	DE
	RET

J1561:	LD	C,00H
	POP	HL
	POP	DE
	RET

J1566:	CALL	C1633
	JR	NC,J1578
	DEC	DE
	LD	A,D
	OR	E
	RET	Z
	CALL	C1426
	JR	NC,J1566
	LD	DE,0
	RET

J1578:	CALL	C1376
	LD	(CSAVEA),HL
	LD	(CSAVEM),A
	LD	HL,0
J1584:	INC	HL
	CALL	C1426
	RET	C
	CALL	C1633
	JR	NC,J1584
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C158F:	LD	HL,0
	LD	C,L
	CALL	C062C			; new style screenmode ?
	JP	C,J1626			; nope,
	CALL	C13E7
	RET	C
	LD	HL,(CLOC)
	PUSH	HL
	LD	A,(CMASK)
	LD	E,A
	LD	D,00H
	LD	A,(BDRATR)
	CALL	C28D8
	PUSH	AF
	JR	C,J15B3
	LD	HL,-1
J15B3:	INC	HL
	LD	(CLOC),HL
	EX	DE,HL
	POP	HL
	EX	(SP),HL
	PUSH	HL
	OR	A
	SBC	HL,DE
	INC	HL
	EX	(SP),HL
	PUSH	DE
	LD	A,(CMASK)
	LD	E,A
	LD	D,00H
	LD	A,(ATRBYT)
	CALL	C28DC
	JR	NC,J161B
	POP	DE
	POP	BC
	POP	AF
	PUSH	BC
	PUSH	DE
	JR	NC,J15D9
	RST	20H
	JR	C,J1621
J15D9:	POP	HL
	POP	DE

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C15DB:	LD	A,36
	DI
	OUT	(VDP_PORT+1),A
	LD	A,080H+17
	OUT	(VDP_PORT+1),A			; set indirect vdp register pointer to 36, autoincrement
	LD	A,L
	OUT	(VDP_PORT+3),A
	LD	A,H
	OUT	(VDP_PORT+3),A
	LD	A,(CMASK)
	OUT	(VDP_PORT+3),A
	LD	A,(ACPAGE)
	OUT	(VDP_PORT+3),A
	LD	A,E
	OUT	(VDP_PORT+3),A
	LD	A,D
	OUT	(VDP_PORT+3),A
	LD	A,01H	; 1 
	OUT	(VDP_PORT+3),A
	XOR	A
	OUT	(VDP_PORT+3),A
	LD	A,(ATRBYT)
	OUT	(VDP_PORT+3),A
	XOR	A
	OUT	(VDP_PORT+3),A
	LD	A,(SCRMOD)
	CP	08H	; 8 
	LD	A,80H
	JR	NZ,J1614
	LD	A,0C0H
J1614:	OUT	(VDP_PORT+3),A
	EI
	EX	DE,HL
	LD	C,0FFH
	RET

J161B:	POP	DE
	POP	HL
	POP	DE
	LD	C,00H
	RET

J1621:	POP	DE
	POP	HL
	LD	C,00H
	RET

J1626:	CALL	C13E7
	RET	C
	CALL	C1633
	JP	C,J1439
	INC	HL
	JR	J1626


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1633:	CALL	C1384
	LD	B,A
	LD	A,(BDRATR)
	SUB	B
	SCF
	RET	Z
	LD	A,(ATRBYT)
	CP	B
	RET	Z
	CALL	C13B5
	LD	C,01H	; 1 
	AND	A
	RET

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1649:	EI
	LD	DE,I1660
	PUSH	HL
	LD	HL,PROCNM
	CALL	C16AD
	POP	HL
	RET	C
	SCF
	RET	NZ
	CALL	C165E
	EI
	AND	A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C165E:	PUSH	DE
	RET

I1660:	DEFB	"MEMINI",0
	DEFW	C3702
	DEFB	"MKILL",0
	DEFW	C38EF
	DEFB	"MNAME",0
	DEFW	C3946
	DEFB	"MFILES",0
	DEFW	C381B
	DEFB	0FFH

;	  Subroutine GET/PUT statement extension
;	     Inputs  ________________________
;	     Outputs ________________________

J1683:	PUSH	AF
	PUSH	HL
	LD	DE,I1699
	CALL	C16AD
	JR	C,J1695
	POP	AF
	POP	AF
	DEC	HL
	CALL	C165E
	AND	A
	RET

J1695:	POP	HL
	POP	AF
	SCF
	RET

I1699:	DEFB	0CBH,0
	DEFW	C16DB
	DEFB	"DATE",0
	DEFW	C16E1
	DEFB	"KANJI",0
	DEFW	C2195
	DEFB	0FFH


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C16AD:	PUSH	HL
	LD	A,(DE)
	INC	A
	JR	Z,J16C6
	CALL	C16C9
	JR	Z,J16BD
	INC	DE
	INC	DE
	INC	DE
	POP	HL
	JR	C16AD

J16BD:	LD	A,(HL)
	EX	(SP),HL
	EX	DE,HL
	INC	HL
	LD	E,(HL)
	INC	HL
	LD	D,(HL)
	AND	A
	DEFB	038H
J16C6:	SCF
	POP	HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C16C9:	LD	A,(DE)
	OR	A
	RET	Z
	CP	(HL)
	JR	NZ,J16D3
	INC	DE
	INC	HL
	JR	C16C9

J16D3:	LD	A,(DE)
	AND	A
	INC	DE
	JR	NZ,J16D3
	DEC	DE
	DEC	A
	RET

;	  Subroutine GET TIME statement
;	     Inputs  ________________________
;	     Outputs ________________________

C16DB:	DEC	B
	LD	BC,I174B
	JR	J16E5

;	  Subroutine GET DATE statement
;	     Inputs  ________________________
;	     Outputs ________________________

C16E1:	DEC	B
	LD	BC,I1767
J16E5:	JP	P,J0546			; illegal function call error
	RST	10H
	PUSH	BC
	CALL	C05D8			; locate variable
	CALL	C05D2			; check for string
	DEC	HL
	RST	10H
	JR	Z,J16FE
	RST	08H
	DEFB	","
	RST	08H
	DEFB	"A"
	EX	(SP),HL
	LD	BC,14
	ADD	HL,BC
	EX	(SP),HL
J16FE:	POP	BC
	PUSH	HL
	PUSH	DE
	PUSH	BC
	LD	A,8
	CALL	C05EA			; allocate temporary string
	LD	HL,(DSCTMP+1)
	POP	DE
	PUSH	HL
	LD	A,(DE)
	RLCA
	AND	01H	; 1 
	LD	B,A
	LD	C,04H
	CALL	C194B			; change RP-501 register 13
	LD	C,02H	; 2 
J1718:	LD	B,02H	; 2 
J171A:	LD	A,(DE)
	INC	A
	JR	Z,J1723
	DEC	A
	OUT	(RTC_PORT),A
	IN	A,(RTC_PORT+1)
J1723:	INC	DE
	EX	DE,HL
	ADD	A,(HL)
	EX	DE,HL
	DEC	DE
	DAA
	AND	0FH	; 15 
	OR	30H	; "0"
	LD	(HL),A
	INC	DE
	INC	DE
	INC	HL
	DJNZ	J171A
	INC	C
	DEC	C
	JR	Z,J173E
	LD	A,(DE)
	LD	(HL),A
	INC	DE
	INC	HL
	DEC	C
	JR	J1718

J173E:
 IF	CNTRY = 0
	CALL	C1947
 ELSE
	CALL	C3CA7
 ENDIF

	POP	DE
	POP	HL
	LD	(HL),08H	; 8 
	INC	HL
	LD	(HL),E
	INC	HL
	LD	(HL),D
	POP	HL
	RET

I174B:	DEFB	05H,00H,04H,00H,":",03H,00H,02H
	DEFB	00H,":",01H,00H,00H,00H,85H,00H
	DEFB	84H,00H,":",83H,00H,82H,00H,":"
	DEFB	81H,00H,80H,00H

I1767:	DEFB	0CH,08H,0BH,00H,"/",0AH,00H,09H
	DEFB	00H,"/",08H,00H,07H,00H,0FFH,00H
	DEFB	0FFH,00H,"/",0FFH
	DEFB	00H,0FFH,00H,"/",88H,00H,87H,00H

;	  Subroutine SET SCREEN statement
;	     Inputs  ________________________
;	     Outputs ________________________

J1783:	RST	10H
	PUSH	HL
	CALL	C1CF7			; select RP-501 bank 2
	LD	B,3
	LD	A,(OLDSCR)
	LD	C,A
	LD	A,(RG9SAV)
	RRCA
	RRCA
	AND	02H
	OR	C
	CALL	C1A59			; write nibble to RP-501 (register 3)
	LD	A,(LINLEN)
	CALL	C1A50			; write byte to RP-501 (register 4 and 5)
	LD	A,(FORCLR)
	CALL	C1A59			; write nibble to RP-501 (register 6)
	LD	A,(BAKCLR)
	CALL	C1A59			; write nibble to RP-501 (register 7)
	LD	A,(BDRCLR)
	CALL	C1A59			; write nibble to RP-501 (register 8)
	LD	C,00H
	LD	A,(CNSDFG)
	OR	A
	JR	Z,J17BB
	SET	0,C
J17BB:	LD	A,(CLIKSW)
	OR	A
	JR	Z,J17C3
	SET	1,C
J17C3:	LD	A,(NTMSXP)
	OR	A
	JR	Z,J17CB
	SET	2,C
J17CB:
	LD	A,(CS1200)
	LD	HL,LOWx
	CP	(HL)
	JR	Z,J17D6
	SET	3,C
J17D6:	LD	A,C
	JP	J1A8C

;	  Subroutine SET ADJUST statement
;	     Inputs  ________________________
;	     Outputs ________________________

C17DA:	RST	08H
	DEFB	"("
	CALL	C180D
	LD	A,E
	PUSH	AF
	RST	08H
	DEFB	","
	CALL	C180D
	RST	08H
	DEFB	")"
	POP	AF
	AND	0FH	; 15 
	LD	D,A
	CALL	C17F8
	CALL	C1CF7			; select RP-501 bank 2
	LD	B,1
	LD	A,D
	JP	C1A50			; write byte to RP-501 (register 1 and 2)


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C17F8:	LD	A,E
	RLCA
	RLCA
	RLCA
	RLCA
	AND	0F0H
	OR	D
	LD	D,A
	LD	(RG18SA),A
	DI
	OUT	(VDP_PORT+1),A
	LD	A,080H+18
	EI
	OUT	(VDP_PORT+1),A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C180D:	CALL	C0608			; evaluate integer operand
	PUSH	HL
	EX	DE,HL
	CALL	C25DC
	EX	DE,HL
	LD	HL,7
	RST	20H
	JR	NC,J1823
	LD	HL,-9
	RST	20H
	JP	NC,J0546		; illegal function call error
J1823:	POP	HL
	RET


;	  Subroutine evaluate string expression
;	     Inputs  ________________________
;	     Outputs ________________________

C1825:	CALL	C05FC			; evaluate expression
	PUSH	HL
	CALL	C0614			; free temporary string with type check
	LD	A,(HL)
	INC	HL
	LD	E,(HL)
	INC	HL
	LD	D,(HL)
	POP	HL
	RET

;	  Subroutine SET statement extension
;	     Inputs  ________________________
;	     Outputs ________________________

J1833:	CP	0C5H
	JP	Z,J1783
	CP	0CBH
	JP	Z,J188A
	CP	0C0H
	JP	Z,J19A6
	LD	DE,I184F
	CALL	C16AD
	JP	C,J0540			; syntax error
	DEC	HL
	RST	10H
	PUSH	DE
	RET

I184F:	DEFB	"PAGE",0
	DEFW	C1FB1
	DEFB	"VIDEO",0
	DEFW	C35AF
	DEFB	"ADJUST",0
	DEFW	C17DA
	DEFB	"TITLE",0
	DEFW	C1980
	DEFB	"PASSW",0F7H,"D",0
	DEFW	C19DD
	DEFB	"PROMPT",0
	DEFW	C19E1
	DEFB	"DATE",0
	DEFW	C188E
	DEFB	0FFH

;	  Subroutine SET TIME statement
;	     Inputs  ________________________
;	     Outputs ________________________

J188A:	RST	10H
	LD	A,":"
	DEFB	1

;	  Subroutine SET DATE statement
;	     Inputs  ________________________
;	     Outputs ________________________

C188E:	LD	A,"/"
	PUSH	AF
	CALL	C1825			; evaluate string expression
	POP	IX
	CP	8			; size of string exactly 8 ?
	JP	NZ,J0540		; nope, syntax error
	PUSH	HL
	EX	DE,HL
	CALL	C195E

 IF	DATFMT = 0
	JR	Z,J18A6
	CP	24H
	JR	NC,J18C0
J18A6:	LD	B,A
	CALL	C1956
	JR	Z,J18B0
	CP	60H
	JR	J18B2

J18B0:	CP	13H
J18B2:	JR	NC,J18C0
	LD	C,A
	CALL	C1956
	JR	Z,J18BE
	CP	60H
	JR	J18C0

J18BE:	CP	32H
J18C0:	JP	NC,J0546		; illegal function call error
	LD	D,A
	POP	HL
	PUSH	IX
	DEC	HL
	RST	10H
	POP	IX
	LD	A,00H
	JR	Z,J18D9
	PUSH	IX
	RST	08H
	DEFB	","
	RST	08H
	DEFB	"A"
	POP	IX
	OR	01H
 ENDIF

 IF	DATFMT = 2
	LD	E,24H
	JR	NZ,J18A7
	LD	D,A
	LD	E,32H
J18A7:	CP	E
	JR	NC,J18C3
	LD	B,A
	CALL	C1956
	LD	E,13H
	JR	Z,J18B4
	LD	E,60H
J18B4:	CP	E
	JR	NC,J18C3
	LD	C,A
	CALL	C1956
	JR	NZ,J18C0
	LD	B,A
	JR	J18C6

J18C0:	LD	D,A
	CP	60H
J18C3:	JP	NC,J0546
J18C6:	POP	HL
	PUSH	IX
	DEC	HL
	RST	10H
	LD	A,0
	JR	Z,J18D5
	RST	08H
	DEFB	","
	RST	08H
	DEFB	",A"
	OR	01H 
J18D5:	POP	IX
	NOP
	NOP
 ENDIF

J18D9:	PUSH	HL
	PUSH	AF
	PUSH	BC
	PUSH	DE
	JR	NZ,J18EA
	LD	BC,0004H
	CALL	C194B			; change RP-501 register 13
	LD	BC,0E00H		; reset alarm registers,  leave fractions, 16 Hz pulse off, 1 Hz pulse off
	JR	J18F8

J18EA:	LD	BC,0108H
	CALL	C194B			; change RP-501 register 13
	CALL	C196C
	JR	Z,J18FD
	LD	BC,0D00H		; reset alarm registers,  leave fractions, 16 Hz pulse off, 1 Hz pulse off
J18F8:	LD	A,15
	CALL	C194D			; change RP-501 register 15
J18FD:	POP	DE
	LD	B,0
	CALL	C196C
	JR	NZ,J1907
	LD	B,7
J1907:	LD	A,D
	CALL	C1A50			; write byte to RP-501
	POP	DE
	POP	AF
	PUSH	AF
	JR	Z,J1915
	CALL	C196C
	JR	Z,J1941
J1915:	LD	A,E
	CALL	C1A50			; write byte to RP-501
	LD	A,D
	CALL	C196C
	JR	NZ,J193E
	PUSH	AF
	PUSH	BC
	BIT	4,A
	JR	Z,J1927
	ADD	A,02H	; 2 
J1927:	AND	03H	; 3 
	PUSH	AF
	LD	A,1
	CALL	C1CF9			; select RP-501 bank 1
	POP	AF
	LD	B,11
	CALL	C1A59			; write nibble to RP-501 (register 11)
	XOR	A
	CALL	C1CF9			; select RP-501 bank 0
	POP	BC
	POP	AF
	SUB	80H
	DAA
J193E:	CALL	C1A50			; write byte to RP-501
J1941:	POP	AF
	POP	HL
	LD	B,04H
	JR	NZ,J1949

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1947:	LD	B,08H
J1949:	LD	C,0FH

;	  Subroutine change RP-501 register 13
;	     Inputs  ________________________
;	     Outputs ________________________

C194B:	LD	A,13

;	  Subroutine change RP-501 register
;	     Inputs  ________________________
;	     Outputs ________________________

C194D:
	OUT	(RTC_PORT),A
	IN	A,(RTC_PORT+1)
	AND	C
	OR	B
	OUT	(RTC_PORT+1),A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1956:	PUSH	IX
	POP	AF
	CP	(HL)
	INC	HL
	JP	NZ,J0540		; syntax error

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C195E:	CALL	C1974
	RLCA
	RLCA
	RLCA
	RLCA
	AND	0F0H
	LD	E,A
	CALL	C1974
	OR	E

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C196C:	PUSH	BC
	PUSH	IX
	POP	BC
	BIT	4,B
	POP	BC
	RET

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1974:	LD	A,(HL)
	INC	HL
	SUB	"0"
	JR	C,J197D
	CP	9+1
	RET	C
J197D:	JP	J0546			; illegal function call error

;	  Subroutine SET TITLE statement
;	     Inputs  ________________________
;	     Outputs ________________________

C1980:	CP	","			; title argument specified ?
	JR	Z,J198B			; nope,
	XOR	A			; select title
	CALL	C19EC			; store title in clockchip
	DEC	HL
	RST	10H
	RET	Z
J198B:	RST	08H
	DEFB	","
	RST	28H			; evaluate byte operand
	RET	NZ
	DEC	A
	CP	4			; value 1-4 ?
	JR	NC,J197D		; nope, illegal function call error
	PUSH	AF
	CALL	C1CF7			; select RP-501 bank 2
	LD	B,11
	CALL	READRTC			; read RP-501 register 11
	AND	0CH
	LD	C,A			; leave b3,b2
	POP	AF
	OR	C			; color in b1,b0
	DEC	B
	JP	C1A59			; write nibble to RP-501 (register 11)

;	  Subroutine SET BEEP statement
;	     Inputs  ________________________
;	     Outputs ________________________

J19A6:	CALL	C1CF7			; select RP-501 bank 2
	LD	B,10
	CALL	READRTC			; read RP-501 register 10
	PUSH	AF
	RST	10H
	CP	","
	JR	Z,J19C8
	RST	28H			; evaluate byte operand
	DEC	A
	CP	4
	JR	NC,J19CE
	RLCA
	RLCA
	AND	0CH
	LD	B,A
	POP	AF
	AND	03H
	OR	B
	PUSH	AF
	DEC	HL
	RST	10H
	JR	Z,J19D7
J19C8:	RST	08H
	DEFB	","
	RST	28H			; evaluate byte operand
	DEC	A
	CP	4
J19CE:	JP	NC,J0546		; illegal function call error
	LD	B,A
	POP	AF
	AND	0CH
	OR	B
	PUSH	AF
J19D7:	LD	B,10
	POP	AF
	JP	C1A59			; write nibble to RP-501 (register 10)

;	  Subroutine SET PASSWORD statement
;	     Inputs  ________________________
;	     Outputs ________________________

C19DD:	LD	A,1			; select password
	JR	J19E3

;	  Subroutine SET PROMPT statement
;	     Inputs  ________________________
;	     Outputs ________________________

C19E1:	LD	A,2			; select prompt
J19E3:	CALL	C19EC			; store title or prompt in clockchip
	DEC	HL
	RST	10H
	JP	NZ,J0540		; syntax error
	RET


;	  Subroutine store title,password or prompt in clockchip
;	     Inputs  ________________________
;	     Outputs ________________________

C19EC:	PUSH	AF
	CALL	C1825			; evaluate string expression
	LD	C,A			; size of string
	CALL	C1CF3			; select RP-501 bank 3
	LD	B,12			; start with register 12
	XOR	A
J19F7:	CALL	C1A59			; write nibble to RP-501
	DEC	B
	DJNZ	J19F7			; clear registers 1-12
	POP	AF			; title/password/prompt
	CALL	C1A59			; write nibble to RP-501 (clear register 0)
	PUSH	HL
	EX	DE,HL
	DEC	A			; password ?
	JR	Z,J1A17			; yep, stored otherwise
	LD	E,6			; 6 characters maximum
J1A08:	DEC	C
	INC	C			; end of string ?
	JR	Z,J1A15			; yep, quit
	LD	A,(HL)
	INC	HL
	CALL	C1A50			; write byte to RP-501
	DEC	C
	DEC	E
	JR	NZ,J1A08		; next
J1A15:	POP	HL
	RET

J1A17:
	LD	A,21H
	CALL	C1A50			; write byte to RP-501
	LD	A,03H
	CALL	C1A59			; write nibble to RP-501
	LD	A,C			; size of password string
	PUSH	AF
	CALL	C1CA5			; calculate checksum
	LD	B,4			; register 4
	LD	A,E
	CALL	C1A50			; write byte to RP-501 (register 4,5)
	LD	A,D
	CALL	C1A50			; write byte to RP-501 (register 6,7)
	POP	AF
	LD	C,A			; size of password string
	XOR	A
	OUT	(MSX2KC_PORT+1),A
	IN	A,(MSX2KC_PORT+1)
	CP	0AAH			; keycard reader found ?
	JR	NZ,J1A15		; nope, quit
	LD	A,C
	OR	A			; empty password string ?
	LD	A,1
	JR	Z,J1A42			; yep, use indicator 1
	INC	A			; nope, use indicator 2
J1A42:
	CALL	C1A59			; write nibble to RP-501 (register 8)
	PUSH	BC
	CALL	C1C97			; read keycard
	POP	BC
	LD	A,E
	CALL	C1A50			; write byte to RP-501 (register 9,10)
	LD	A,D
	POP	HL
					; write byte to RP-501 (register 11,12)

;	  Subroutine write byte to RP-501
;	     Inputs  ________________________
;	     Outputs ________________________

C1A50:
	PUSH	AF
	CALL	C1A59			; write nibble to RP-501
	POP	AF
	RRCA
	RRCA
	RRCA
	RRCA
					; write nibble to RP-501

;	  Subroutine write nibble to RP-501
;	     Inputs  ________________________
;	     Outputs ________________________

C1A59:
	PUSH	AF
	LD	A,B
	OUT	(RTC_PORT),A
	INC	B
	POP	AF
	OUT	(RTC_PORT+1),A
	RET

J1A62:	PUSH	HL
	CALL	C1AD0
	PUSH	AF
	DEC	B
	JR	Z,J1A90
	DEC	B
	JR	Z,J1AAD
	DEC	B
	JR	Z,J1ABC
	LD	E,55H	; "U"
	CALL	C1B11
	CALL	C1B1A
	CALL	C1B32
	LD	A,08H	; 8 
	CALL	C1B1E
	LD	BC,07D0H
	CALL	C1B28
J1A86:	CALL	C056B
	POP	AF
	LD	B,13			; register 13
J1A8C:	POP	HL
	JP	C1A59			; write nibble to RP-501

J1A90:	LD	HL,I1B3E
	LD	E,0B8H
	CALL	C1B1C
	LD	E,0A7H
	LD	A,02H	; 2 
	CALL	C1B12
	LD	E,0A8H
	LD	A,04H	; 4 
	CALL	C1B12
	LD	E,0A6H
J1AA8:	CALL	C1AEA
	JR	J1A86

J1AAD:	LD	HL,I1B3E
	CALL	C1B1A
	LD	E,2AH	; "*"
	CALL	C1AEA
	LD	E,35H	; "5"
	JR	J1AA8

J1ABC:	LD	HL,I1B46
	CALL	C1B1A
	LD	E,6BH	; "k"
	CALL	C1AEA
	LD	E,47H	; "G"
	CALL	C1AEA
	LD	E,55H	; "U"
	JR	J1AA8


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1AD0:
	LD	B,13
	CALL	READRTC			; read RP-501 register 13
	PUSH	AF
	CALL	C1CF7			; select RP-501 bank 2
	LD	B,10
	CALL	READRTC			; read RP-501 register 10
	LD	C,A
	RRCA
	RRCA
	AND	03H	; 3 
	LD	B,A
	LD	A,C
	AND	03H	; 3 
	LD	C,A
	POP	AF
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1AEA:
	CALL	C1B11
	PUSH	BC
	PUSH	HL
	CALL	C1B32
	POP	HL
	PUSH	HL
	ADD	HL,BC
	ADD	HL,BC
	LD	C,(HL)
	INC	HL
	LD	B,(HL)
J1AF9:	LD	A,08H	; 8 
	CALL	C1B1E
	LD	A,09H	; 9 
	CALL	C1B1E
	LD	A,0AH	; 10 
	CALL	C1B1E
	CALL	C1B28
	DEC	E
	JR	NZ,J1AF9
	POP	HL
	POP	BC
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1B11:	XOR	A

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1B12:	CALL	C1B1E
	LD	E,00H
	INC	A
	JR	C1B1E


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1B1A:	LD	E,0BEH

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1B1C:	LD	A,07H	; 7 

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1B1E:	DI
	OUT	(PSG_PORT),A
	PUSH	AF
	LD	A,E
	EI
	OUT	(PSG_PORT+1),A
	POP	AF
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1B28:	PUSH	BC
J1B29:	DEC	BC
	EX	(SP),HL
	EX	(SP),HL
	LD	A,B
	OR	C
	JR	NZ,J1B29
	POP	BC
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1B32:	LD	B,00H
	LD	HL,I1B3A
	ADD	HL,BC
	LD	E,(HL)
	RET

I1B3A:	DEFB	04H,07H,0AH,0FH

I1B3E:	DEFW	010D8H
	DEFW	009A0H
	DEFW	006BDH
	DEFW	0047EH

I1B46:	DEFW	0059DH
	DEFW	00335H
	DEFW	0023FH
	DEFW	0017FH

J1B4E:	CALL	C05AE			; fresh line to interpreter output
	CALL	C1CF3			; select RP-501 bank 3
	CALL	C1CD0			; read RP-501 register 0
	CP	02H
	LD	HL,I3FD7
	JP	NZ,C05F6		; message to interpreter output
J1B5F:	CALL	C1CDB			; read byte from RP-501 registers
	JR	Z,J1B6A
	RST	18H
	LD	A,B
	CP	0DH
	JR	C,J1B5F
J1B6A:	JP	J05A8			; newline to interpreter output

J1B6D:
	CALL	C1C73			; read VRAM address 01FFFFH
	LD	C,A
	CPL
	CALL	C1C74			; write VRAM address 01FFFFH
	CALL	C1C73			; read VRAM address 01FFFFH
	CPL
	CP	C			; address has VRAM ?
	LD	A,C
	CALL	C1C74			; write VRAM address 01FFFFH
	LD	HL,I1C64
	LD	C,04H
	JR	Z,J1B8A			; yep, system has 128 KB VRAM (4 pages for screenmode 5 and 6)
	LD	HL,I1C68
	LD	C,02H			; nope, system has 64 KB VRAM (2 pages for screenmode 5 and 6)
J1B8A:
	PUSH	HL
	LD	A,(MODE)
	AND	0F9H
	OR	C			; VRAM size in b2,b1
	LD	(MODE),A

	LD	HL,00060H						; coluna
	LD	A,0B4H							; linha
	CALL	C1D2B						; locate
	LD	HL,I1C5E
	CALL	C1C4B			; print vram message on graphic screen
	POP	HL
	CALL	C1C4B			; print vram size message on graphic screen
	LD	HL,I1C6C
	CALL	C1C4B			; print kb message on graphic screen

	IF	CNTRY != 0
													; by Fabio - adicionando impressao do tam. da RAM
		ld		hl, $0060					; coluna
		LD		a, $BC						; linha
		CALL	C1D2B						; locate
		LD		HL,I2DA4					; String 
		CALL	C1C4B						; Imprime string
	
		pop		hl							; retira da pilha o tamanho da RAM em blocos de 16K
		add		hl, hl						; multiplica por 4
		add		hl, hl
		add		hl, hl
		add		hl, hl
		CALL	NUM_TO_STR
		CALL	C1C4B						; Imprime string
		LD		HL,I1C6C					; String 
		CALL	C1C4B						; Imprime string
	ENDIF

	XOR	A
	OUT	(VDP_PORT+1),A
	OUT	(VDP_PORT+1),A			; VRAM address pointer=0
	OUT	(VDP_PORT+1),A
	LD	A,080H+14
	OUT	(VDP_PORT+1),A			; vdp register 14=0
	CALL	C1CF3			; select RP-501 bank 3
	CALL	C1CD0			; read RP-501 register 0
	JR	Z,J1C35
	DEC	A
	RET	NZ
	CALL	READRTC			; read RP-501 register 1
	CP	01H
	RET	NZ
	CALL	READRTC			; read RP-501 register 2
	CP	02H
	RET	NZ
	CALL	READRTC			; read RP-501 register 3
	CP	03H
	RET	NZ
	LD	B,8
	CALL	READRTC			; read RP-501 register 8
	JR	Z,J1BEF
	XOR	A
	OUT	(MSX2KC_PORT+1),A
	IN	A,(MSX2KC_PORT+1)
	CP	0AAH
	JR	NZ,J1BEF
	PUSH	BC
	CALL	C1C97			; read keycard
	POP	BC
	CALL	C1CEA			; read word from RP-501 registers 9-12
	RST	20H
	RET	Z
	JR	J1C00

J1BEF:	LD	A,6
	CALL	C0590			; read keyboard row 6
	AND	04H
	JR	NZ,J1C00
	LD	A,7
	CALL	C0590			; read keyboard row 7
	AND	10H
	RET	Z
J1C00:	LD	HL,0032H
	LD	A,0D0H
	CALL	C1D2B
	LD	HL,I1C54
	CALL	C1C4B			; print message on graphic screen
	LD	B,8
	CALL	READRTC			; read RP-501 register 8
	DEC	A
J1C14:	JR	Z,J1C14
J1C16:	LD	HL,BUF			; use BUF for inputline
	LD	B,255
J1C1B:	CALL	C0565			; get keyboard input
	CP	0DH
	JR	Z,J1C26
	LD	(HL),A
	INC	HL
	DJNZ	J1C1B
J1C26:	LD	A,255
	SUB	B			; size of line
	CALL	C1CA2			; calculate checksum of BUF
	LD	B,4
	CALL	C1CEA			; read word from RP-501 registers 4-7
	RST	20H
	RET	Z
	JR	J1C16

J1C35:	LD	HL,00C8H
	LD	A,L
	CALL	C1D2B
J1C3C:	CALL	C1CDB			; read byte from RP-501 registers
	RET	Z
	CALL	C1224			; print char on graphic screen
	LD	A,B
	CP	0DH
	JR	C,J1C3C
	JP	C0565			; get keyboard input


;	  Subroutine print message on graphic screen
;	     Inputs  ________________________
;	     Outputs ________________________

C1C4B:	LD	A,(HL)
	INC	HL
	OR	A
	RET	Z
	CALL	C1224			; print char on graphic screen
	JR	C1C4B

I1C54:
	DEFB	"Password:"
	DEFB	0

I2DA4:
	DEFM	' Main RAM:', 0

I1C5E:
	DEFB	"Video RAM: "
	DEFB	0

I1C64:
	DEFB	" 128"
	DEFB	0

I1C68:
	DEFB	"  64"
	DEFB	0

I1C6C:
	DEFB	"Kbytes"
	DEFB	0


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1C73:
	DEFB	0F6H

C1C74:
	SCF
	PUSH	AF
	LD	A,07H
	OUT	(VDP_PORT+1),A
	LD	A,080H+14
	OUT	(VDP_PORT+1),A
	LD	A,0FFH
	OUT	(VDP_PORT+1),A
	POP	AF
	PUSH	AF
	LD	A,7FH
	JR	C,J1C8A
	LD	A,3FH
J1C8A:
	OUT	(VDP_PORT+1),A
	EX	(SP),HL
	EX	(SP),HL
	POP	AF
	JR	C,J1C94
	IN	A,(VDP_PORT)
	RET
J1C94:
	OUT	(VDP_PORT),A
	RET

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1C97:	IN	A,(MSX2KC_PORT+1)
	LD	L,A
	IN	A,(MSX2KC_PORT+1)
	LD	H,A
	LD	(BUF+0),HL
	LD	A,2			; size of "line" = 2

;	  Subroutine calculate checksum of BUF
;	     Inputs  ________________________
;	     Outputs ________________________

C1CA2:	LD	HL,BUF

;	  Subroutine calculate checksum
;	     Inputs  ________________________
;	     Outputs ________________________

C1CA5:	LD	DE,0
	LD	C,A
	OR	A
	RET	Z
	SCF
J1CAC:	PUSH	AF
J1CAD:	LD	B,8
J1CAF:	POP	AF
	PUSH	AF
	JR	NC,J1CB5
	RLC	(HL)
J1CB5:	RL	E
	RL	D
	JR	NC,J1CC3
	LD	A,D
	XOR	80H
	LD	D,A
	LD	A,E
	XOR	05H
	LD	E,A
J1CC3:	DJNZ	J1CAF
	INC	HL
	DEC	C
	JR	NZ,J1CAD
	POP	AF
	RET	NC
	OR	A
	LD	C,02H
	JR	J1CAC


;	  Subroutine read RP-501 register 0
;	     Inputs  ________________________
;	     Outputs ________________________

C1CD0:	LD	B,0

;	  Subroutine read RP-501 register
;	     Inputs  ________________________
;	     Outputs ________________________

READRTC:
	call	TESTRTC						; RTC exist?
	jr nz,	RTCSIMUL					; no, simulate it
	ld		a, b
	out		(RTC_PORT), a
	inc		b
	in		a, (RTC_PORT+1)
	and		$0F
	ret

RTCSIMUL:
	push	hl
	ld		hl, I04FF
	ld		a, b
	add		a, l
	ld		l, a
	ld		a, (hl)
	inc		b
	pop		hl
	ret


;	  Subroutine read byte from RP-501 registers
;	     Inputs  ________________________
;	     Outputs ________________________

C1CDB:	PUSH	DE
	CALL	READRTC			; read RP-501 register
	LD	D,A
	CALL	READRTC			; read RP-501 register
	RLCA
	RLCA
	RLCA
	RLCA
	OR	D
	POP	DE
	RET


;	  Subroutine read word from RP-501 registers
;	     Inputs  ________________________
;	     Outputs ________________________

C1CEA:	CALL	C1CDB			; read byte from RP-501 registers
	LD	L,A
	CALL	C1CDB			; read byte from RP-501 registers
	LD	H,A
	RET


;	  Subroutine select RP-501 bank 3
;	     Inputs  ________________________
;	     Outputs ________________________

C1CF3:	LD	A,3
	JR	C1CF9			; select RP-501 bank 3


;	  Subroutine select RP-501 bank 2
;	     Inputs  ________________________
;	     Outputs ________________________

C1CF7:	LD	A,2

;	  Subroutine select RP-501 bank
;	     Inputs  ________________________
;	     Outputs ________________________

C1CF9:	PUSH	BC
	PUSH	AF
	LD	B,13
	CALL	READRTC			; read RP-501 register 13
	AND	0CH
	POP	BC
	OR	B
	OUT	(RTC_PORT+1),A
	POP	BC
	RET

C1D08:	CALL	C1D1A
	IN	A,(RTC_PORT+1)
	AND	0FH
	RET

C1D10:	PUSH	AF
	CALL	C1D1A
	POP	AF
	AND	0FH
	OUT	(RTC_PORT+1),A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1D1A:	LD	A,C
	PUSH	AF
	AND	30H
	RRCA
	RRCA
	RRCA
	RRCA
	CALL	C1CF9			; select RP-501 bank
	POP	AF
	AND	0FH
	OUT	(RTC_PORT),A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1D2B:	LD	(CLOC),HL
	LD	(CMASK),A
	RET

J1D32:	LD	A,(HL)
	EX	(SP),HL
	CP	(HL)
	JP	NZ,J0540		; syntax error
	INC	HL
	EX	(SP),HL
J1D3A:	CALL	H.CHRG
	INC	HL
J1D3E:	LD	A,(HL)
	CP	3AH	; ":"
	RET	NC
	CP	20H	; " "
	JR	Z,J1D3A
	JR	NC,J1DB1
	OR	A
	RET	Z
	CP	0BH	; 11 
	JR	C,J1DAC
	CP	1EH
	JR	NZ,J1D57
	LD	A,(CONSAV)
	OR	A
	RET

J1D57:	CP	10H	; 16 
	JR	Z,J1D8F
	PUSH	AF
	INC	HL
	LD	(CONSAV),A
	SUB	1CH
	JR	NC,J1D94
	SUB	0F5H
	JR	NC,J1D6E
	CP	0FEH
	JR	NZ,J1D82
	LD	A,(HL)
	INC	HL
J1D6E:	LD	(CONTXT),HL
	LD	H,00H
J1D73:	LD	L,A
	LD	(CONLO),HL
	LD	A,02H	; 2 
	LD	(CONTYP),A
	LD	HL,I46E6
	POP	AF
	OR	A
	RET

J1D82:	LD	A,(HL)
	INC	HL
	INC	HL
	LD	(CONTXT),HL
	DEC	HL
	LD	H,(HL)
	JR	J1D73

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________
;	Remark	NOT USED

D1D8C:	CALL	C1DB7

J1D8F:	LD	HL,(CONTXT)
	JR	J1D3E

J1D94:	INC	A
	RLCA
	LD	(CONTYP),A
	PUSH	DE
	PUSH	BC
	LD	DE,CONLO
	CALL	C1DF0
	POP	BC
	POP	DE
	LD	(CONTXT),HL
	POP	AF
	LD	HL,I46E6
	OR	A
	RET

J1DAC:	CP	09H	; 9 
	JP	NC,J1D3A
J1DB1:	CP	30H	; "0"
	CCF
	INC	A
	DEC	A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1DB7:	LD	A,(CONSAV)
	CP	0FH	; 15 
	JR	NC,J1DD1
	CP	0DH	; 13 
	JR	C,J1DD1
	LD	HL,(CONLO)
	JR	NZ,J1DCE
	INC	HL
	INC	HL
	INC	HL
	LD	E,(HL)
	INC	HL
	LD	D,(HL)
	EX	DE,HL
J1DCE:	JP	C05DE			; convert to SGN and put in DAC

J1DD1:	LD	A,(CONTYP)
	LD	(VALTYP),A
	CP	02H	; 2 
	JR	NZ,J1DE1
	LD	HL,(CONLO)
	LD	(DAC+2),HL
J1DE1:	LD	HL,CONLO
	LD	DE,DAC
	LD	A,(VALTYP)
	CP	04H	; 4 
	JR	NC,C1DF0
	INC	DE
	INC	DE

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1DF0:	LD	C,A
	LD	B,00H
	LDIR
	RET

J1DF6:	JP	Z,J1ED5
	CP	0EFH
	JP	Z,J1ECA
	CP	0C7H
I1E00:	JP	Z,J2040
	LD	DE,(FORCLR)
	PUSH	DE
	CP	","
	JR	Z,J1E17
	RST	28H			; evaluate byte operand
	CALL	C1E40
	POP	DE
	LD	E,A
	PUSH	DE
	DEC	HL
	RST	10H
	JR	Z,J1E33
J1E17:	RST	08H
	DEFB	","
	CP	","
	JR	Z,J1E28
	RST	28H			; evaluate byte operand
	CALL	C1E40
	POP	DE
	LD	D,A
	PUSH	DE
	DEC	HL
	RST	10H
	JR	Z,J1E33
J1E28:	RST	08H
	DEFB	","
	RST	28H			; evaluate byte operand
	CALL	C1E40
	LD	(BDRCLR),A
	DEC	HL
	RST	10H
J1E33:	EX	(SP),HL
	LD	(FORCLR),HL
	LD	A,L
	LD	(ATRBYT),A
	CALL	C0953			; change colors
	POP	HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1E40:	CALL	C1E47
	RET	NC
	JP	J0546			; illegal function call error


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1E47:	PUSH	AF
	LD	A,(SCRMOD)
	CP	06H	; 6 
	JR	Z,J1E5B
	CP	08H	; 8 
	JR	Z,J1E58
	POP	AF
	CP	10H	; 16 
	CCF
	RET

J1E58:	POP	AF
	AND	A
	RET

J1E5B:	POP	AF
	CP	20H	; " "
	CCF
	RET	C
	CP	10H	; 16 
	JR	C,J1E67
	AND	0FH	; 15 
	RET

J1E67:	AND	03H	; 3 
	PUSH	BC
	LD	B,A
	ADD	A,A
	ADD	A,A
	ADD	A,B
	POP	BC
	RET

J1E70:	RST	10H
	LD	A,0FH	; 15 
	CALL	C1EDC
	PUSH	AF
	CALL	C0F6A			; get pallette values of color (GETPLT)
	PUSH	BC
	RST	08H
	DEFB	","
	CP	","
	JR	Z,J1E98
	LD	A,07H	; 7 
	CALL	C1EDC
	ADD	A,A
	ADD	A,A
	ADD	A,A
	ADD	A,A
	LD	E,A
	POP	BC
	LD	A,B
	AND	0FH	; 15 
	OR	E
	LD	B,A
	PUSH	BC
	DEC	HL
	RST	10H
	CP	40+1
	JR	Z,J1EBA
J1E98:	RST	08H
	DEFB	","
	CP	","
	JR	Z,J1EAC
	LD	A,07H	; 7 
	CALL	C1EDC
	POP	BC
	LD	C,A
	PUSH	BC
	DEC	HL
	RST	10H
	CP	40+1
	JR	Z,J1EBA
J1EAC:	RST	08H
	DEFB	","
	LD	A,07H	; 7 
	CALL	C1EDC
	POP	BC
	LD	A,B
	AND	0F0H
	OR	E
	LD	B,A
	PUSH	BC
J1EBA:	RST	08H
	DEFB	")"
	POP	BC
	LD	A,B
	LD	E,C
	POP	BC
	LD	D,B
	PUSH	AF
	CALL	C1034			; wait for VR to pass
	POP	AF
	CALL	C0FD3			; set pallette values of color (SETPLT)
	RET

J1ECA:	RST	10H
	CP	28H	; "("
	JR	Z,J1E70
	CP	8CH
	JR	Z,J1ED8
	RST	08H
	DEFB	094H
J1ED5:	JP	J0F9F			; initialize pallette (INIPLT)

J1ED8:	RST	10H
	JP	J0F7E			; restore pallette (RSTPLT)


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1EDC:	PUSH	AF
	RST	28H			; evaluate byte operand
	POP	AF
	CP	E
	LD	A,E
	RET	NC
	JP	J0546			; illegal function call error

J1EE5:	CP	","
	JR	Z,J1F1D
	RST	28H			; evaluate byte operand
	CP	8+1
	JP	NC,J0546		; illegal function call error
	CP	5			; new style MSX2 screenmode ?
	JR	C,J1F0E			; nope, 16 Kb VRAM is always enough
	PUSH	AF
	LD	A,(MODE)
	RRCA
	AND	03H			; VRAM size (number of pages for screenmode 5 and 6)
	LD	C,A
	POP	AF
	CP	7			; screenmode 7 or 8 ?
	PUSH	AF
	LD	A,C
	JR	NC,J1F08		; yep, check for VRAM size 128 Kb
	CP	1			; VRAM size 16 Kb ?
	JR	C,J1F0A			; yep, illegal function call error
	JR	J1F0D

J1F08:	CP	2			; VRAM size 128 Kb ?
J1F0A:	JP	C,J0546			; nope, illegal function call error
J1F0D:	POP	AF
J1F0E:	PUSH	HL
	CALL	C09BF			; change screenmode, initialize pallette
	LD	A,(LINLEN)
	LD	E,A
	CALL	C2033
	POP	HL
	DEC	HL
	RST	10H
	RET	Z
J1F1D:	RST	08H
	DEFB	","
	CP	","
	JR	Z,J1F3A
	RST	28H			; evaluate byte operand
	CP	04H	; 4 
	JP	NC,J0546		; illegal function call error
	LD	A,(RG1SAV)
	AND	0FCH
	OR	E
	LD	(RG1SAV),A
	PUSH	HL
	CALL	C06F5			; clear sprites
	POP	HL
	DEC	HL
	RST	10H
	RET	Z
J1F3A:	RST	08H
	DEFB	","
	CP	","
	JR	Z,J1F45
	RST	28H			; evaluate byte operand
	LD	(CLIKSW),A
	RET	Z
J1F45:	RST	08H
	DEFB	","
	CP	","
	JR	Z,J1F51
	CALL	C1F7A
	DEC	HL
	RST	10H
	RET	Z
J1F51:	RST	08H
	DEFB	","
	CP	","
	JR	Z,J1F5C
	RST	28H			; evaluate byte operand
	LD	(NTMSXP),A
	RET	Z
J1F5C:	RST	08H
	DEFB	","
	RST	28H			; evaluate byte operand
	CP	04H	; 4 
	JP	NC,J0546		; illegal function call error
	ADD	A,A
	BIT	1,A
	JR	Z,J1F6B
	SET	3,A
J1F6B:	AND	0CH	; 12 
	LD	B,A
	LD	A,(RG9SAV)
	AND	0F3H
	OR	B
	LD	C,9			; vdp register 9
J1F76:	LD	B,A
	JP	C0647			; write vdp register


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1F7A:	RST	28H			; evaluate byte operand
	DEC	A
	CP	02H	; 2 
	JP	NC,J0546		; illegal function call error
	PUSH	HL
	LD	BC,5
	AND	A
	LD	HL,CS1200
	JR	Z,J1F8C
	ADD	HL,BC
J1F8C:
	LD	DE,LOWx
	LDIR
	POP	HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C1F93:	PUSH	BC
	LD	B,A
	CALL	C062C			; new style screenmode ?
	JR	C,J1FAE			; nope,
	CP	7			; screenmode 7 or 8 ?
	LD	A,2-1
	JR	NC,J1FAD		; yep, 2 pages
	LD	A,(MODE)
	AND	06H
	CP	04H			; VRAM size 128 Kb ?
	LD	A,2-1
	JR	C,J1FAD			; nope, 2 pages (screenmode 5 or 6)
	LD	A,4-1			; yep, 4 pages (screenmode 5 or 6)
J1FAD:	CP	B
J1FAE:	LD	A,B
	POP	BC
	RET

;	  Subroutine SET PAGE statement
;	     Inputs  ________________________
;	     Outputs ________________________

C1FB1:	LD	DE,(DPPAGE)
	CP	","
	JR	Z,J1FC7
	PUSH	DE
	RST	28H			; evaluate byte operand
	POP	DE
	CALL	C1F93
J1FBF:	JP	C,J0546			; illegal function call error
	LD	E,A
	DEC	HL
	RST	10H
	JR	Z,J1FD2
J1FC7:	PUSH	DE
	RST	08H
	DEFB	","
	RST	28H			; evaluate byte operand
	POP	DE
	CALL	C1F93
	JR	C,J1FBF
	LD	D,A
J1FD2:	LD	(DPPAGE),DE
	PUSH	HL
	CALL	C2980			; wait for vdp command to complete
	CALL	C06A8			; set active/display page
	POP	HL
	RET

J1FDF:	JP	Z,J0546			; illegal function call error
	LD	A,(OLDSCR)
	AND	A
	LD	A,E
	JR	Z,J1FEE
	CP	32+1
	JP	NC,J0546		; illegal function call error
J1FEE:	CP	40+1
	JR	C,J1FF7
	CP	80+1
	JP	NC,J0546		; illegal function call error
J1FF7:	LD	A,(LINLEN)
	CP	E
	RET	Z
	LD	A,12
	RST	18H			; clear screen

J1FFF:	LD	A,E
I2000:	LD	(LINLEN),A
	CALL	C2033
	LD	A,(OLDSCR)
	DEC	A
	LD	A,E
	JR	NZ,J2014
	LD	(LINL32),A
	LD	A,12
	RST	18H			; clear screen
	RET

J2014:	LD	C,40+1
	LD	A,(LINL40)
	CP	C
	LD	A,E
	LD	(LINL40),A
	PUSH	AF
	LD	A,12
	RST	18H			; clear screen
	POP	AF
	JR	C,J2028
	CP	C
	RET	NC
	LD	C,A
J2028:	CP	C
	RET	C
	PUSH	AF
	PUSH	HL
	XOR	A			; screenmode 0
	CALL	C09BF			; change screenmode, initialize pallette
	POP	HL
	POP	AF
	LD	E,A

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2033:	SUB	0EH	; 14 
	JR	NC,C2033
	ADD	A,1CH
	CPL
	INC	A
	ADD	A,E
	LD	(CLMLST),A
	RET

J2040:	CALL	C0626			; MSX1 screenmode ?
	JP	C,J0546			; yep, illegal function call error
	RST	10H
	CP	"$"
	JR	Z,J2065
	LD	A,1FH
	CALL	C2318
	PUSH	HL
	CALL	C0775			; get spriteplane colortable
	EX	(SP),HL
	RST	08H
	DEFB	0EFH
	RST	28H			; evaluate byte operand
	AND	A
	JP	M,J0546			; illegal function call error
	LD	BC,16
	EX	(SP),HL
	CALL	C0977			; fill VRAM (active page)
	POP	HL
	RET

J2065:	RST	08H
	DEFB	"$"
	LD	A,1FH
	CALL	C2318
	PUSH	HL
	CALL	C0775			; get spriteplane colortable
	EX	(SP),HL
	RST	08H
	DEFB	0EFH
	CALL	C05FC			; evaluate expression
	PUSH	HL
	CALL	C0614			; free temporary string with type check
	LD	A,(HL)
	CP	11H	; 17 
	JR	C,J2081
	LD	A,10H	; 16 
J2081:	INC	HL
	LD	E,(HL)
	INC	HL
	LD	D,(HL)
	POP	HL
	EX	(SP),HL
	AND	A
	LD	B,A
	EX	DE,HL
	CALL	NZ,C0F2B		; write string to VRAM
	POP	HL
	RET

J208F:	RST	10H
	RST	28H			; evaluate byte operand
	CP	32
	JP	NC,J0546		; illegal function call error
	LD	(BUF+2),A
	PUSH	HL
	PUSH	AF
	CALL	C0775			; get spriteplane colortable
	LD	(LINWRK+0),HL
	LD	DE,LINWRK+2
	LD	B,16
	CALL	C0F22			; read string from VRAM
	POP	AF
	PUSH	AF
	CALL	C076A			; get spriteplane address
	LD	(BUF+0),HL
	POP	AF
	LD	B,A
	LD	A,20H	; " "
	SUB	B
	LD	B,A
	LD	(BUF+3),A
	LD	DE,BUF+4
	PUSH	DE
	CALL	C0F22			; read string from VRAM
	POP	HL
	EX	(SP),HL
	RST	08H
	DEFB	","
	CP	","
	JR	Z,J20F5
	CALL	C059C			; evaluate complex graphic coordinatepair
	EX	(SP),HL
	LD	(HL),E
	INC	HL
	LD	A,B
	ADD	A,A
	LD	A,C
	LD	C,00H
	JR	NC,J20DA
	ADD	A,20H	; " "
	LD	C,80H
J20DA:	LD	(HL),A
	PUSH	HL
	LD	HL,LINWRK+2
I20DF:	LD	B,10H	; 16 
J20E1:	LD	A,(HL)
	AND	7FH
	OR	C
	LD	(HL),A
	INC	HL
	DJNZ	J20E1
	CALL	C217A
	POP	HL
	INC	HL
	EX	(SP),HL
	DEC	HL
	RST	10H
	POP	BC
	JR	Z,J212E
	PUSH	BC
J20F5:	RST	08H
	DEFB	","
	CP	","
	JR	Z,J2119
	RST	28H			; evaluate byte operand
	AND	A
	JP	M,J0546			; illegal function call error
	PUSH	HL
	LD	HL,LINWRK+2
	LD	C,A
	LD	B,16
J2107:	LD	A,(HL)
	AND	80H
	OR	C
	LD	(HL),A
	INC	HL
	DJNZ	J2107
	CALL	C217A
	POP	HL
	DEC	HL
	RST	10H
	POP	BC
	JR	Z,J212E
	PUSH	BC
J2119:	RST	08H
	DEFB	","
	RST	28H			; evaluate byte operand
	CALL	C077F			; get spritesize
	LD	A,E
	JR	NC,J2129
	CP	64
	JP	NC,J0546		; illegal function call error
	ADD	A,A
	ADD	A,A
J2129:	EX	(SP),HL
	LD	(HL),A
	LD	B,H
	LD	C,L
	POP	HL
J212E:	PUSH	HL
	LD	A,(BUF+2)
	SUB	1FH
	JR	Z,J216A
	LD	HL,(LINWRK+0)
	LD	A,(BUF+3)
	LD	B,A
J213D:	LD	DE,16
	ADD	HL,DE
	PUSH	HL
	PUSH	BC
	LD	DE,LINWRK+2
	LD	B,16
	CALL	C0F22			; read string from VRAM
	CALL	C2187
	POP	BC
	POP	HL
	JR	Z,J2154
	DJNZ	J213D
J2154:	LD	A,(BUF+3)
	SUB	B
	JR	Z,J216A
	LD	B,A
	LD	HL,BUF+8
	LD	DE,(BUF+4)
J2162:	LD	(HL),E
	INC	HL
	LD	(HL),D
	INC	HL
	INC	HL
	INC	HL
	DJNZ	J2162
J216A:	INC	A
	ADD	A,A
	ADD	A,A
	LD	B,A
	LD	HL,BUF+4
	LD	DE,(BUF)
	CALL	C0F2B			; write string to VRAM
	POP	HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C217A:	LD	HL,LINWRK+2
	LD	DE,(LINWRK+0)
	LD	B,16
	CALL	C0F2B			; write string to VRAM
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2187:	LD	HL,LINWRK+2
	LD	B,16
J218C:	LD	A,(HL)
	AND	40H	; "@"
	RET	NZ
	INC	HL
	DJNZ	J218C
	XOR	A
	RET

;	  Subroutine PUT KANJI statement
;	     Inputs  ________________________
;	     Outputs ________________________

C2195:	DEC	B
	JP	M,J0540			; syntax error
	LD	A,(SCRMOD)
	CP	05H	; 5 
	JP	C,J0546			; illegal function call error
	RST	10H
	CP	","
	CALL	NZ,C059C		; evaluate complex graphic coordinatepair
	RST	08H
	DEFB	","
	CALL	C0608			; evaluate integer operand
	PUSH	DE
	CALL	C256F
	CALL	C2594
	DEC	HL
	RST	10H
	CP	","
	LD	A,00H
	JR	NZ,J21C2
	RST	10H
	RST	28H			; evaluate byte operand
	CP	03H	; 3 
	JP	NC,J0546		; illegal function call error
J21C2:	POP	BC
	JP	J1049

J21C6:	LD	A,2FH	; "/"
	CALL	C2318
	CP	08H	; 8 
	JR	C,J21DD
	JP	Z,J0546			; illegal function call error
	DEC	E
	DEC	A
	CP	18H
	JR	C,J21DD
	CP	20H	; " "
	JP	C,J0546			; illegal function call error
J21DD:	PUSH	DE
	RST	08H
	DEFB	0EFH
	RST	28H			; evaluate byte operand
	POP	BC
	JP	J1F76

J21E5:	RST	10H
	RST	08H
	DEFB	"("
	CALL	C0608
	RST	08H
	DEFB	")"
	PUSH	HL
	LD	A,D
	RLCA
	JR	C,J220E
	AND	A
	JR	NZ,J21F8
	LD	A,E
	CP	19H
J21F8:	JP	NC,J0546		; illegal function call error
	CP	09H	; 9 
	JR	NC,J2204
	LD	HL,RG0SAV
	JR	J2207

J2204:	LD	HL,RG8SAV-8-1
J2207:	ADD	HL,DE
	LD	A,(HL)
J2209:	CALL	C05E4			; byte to DAC
	POP	HL
	RET

J220E:	EX	DE,HL
	CALL	C25DC
	EX	DE,HL
	LD	HL,9
	RST	20H
	JP	C,J0546			; illegal function call error
	LD	A,E
	CALL	C298B			; read vdp status register
	JR	J2209

J2220:	LD	A,13H	; 19 
	CALL	C2318
	LD	D,00H
	PUSH	DE
	RST	08H
	DEFB	0EFH
	CALL	C0608			; evaluate integer operand
	CP	40H	; "@"
	JP	NC,J0546		; illegal function call error
	EX	(SP),HL
	PUSH	HL
	LD	C,E
	LD	B,D
	POP	HL
	LD	A,L
	PUSH	AF
	ADD	HL,HL
	EX	DE,HL
	LD	HL,I226F
	ADD	HL,DE
	LD	A,C
	AND	(HL)
	JR	NZ,J2246
	INC	HL
	LD	A,B
	AND	(HL)
J2246:	JP	NZ,J0546		; illegal function call error
	LD	HL,TXTNAM
	ADD	HL,DE
	LD	(HL),C
	INC	HL
	LD	(HL),B
	POP	AF
	LD	E,0FFH
J2253:	INC	E
	SUB	05H	; 5 
	JR	NC,J2253
	LD	A,(SCRMOD)
	CP	E
	CALL	Z,C2261
	POP	HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2261:	DEC	A
	JP	M,C0B42			; SETTXT
	JP	Z,C0B9C			; SETT32
	DEC	A
	JP	Z,C0BD2			; SETGRP
	JP	C0C22			; SETMLT

I226F:	DEFW	003FFH
	DEFW	0003FH
	DEFW	007FFH
	DEFW	0007FH
	DEFW	007FFH
	DEFW	003FFH
	DEFW	0003FH
	DEFW	007FFH
	DEFW	0007FH
	DEFW	007FFH
	DEFW	003FFH
	DEFW	01FFFH
	DEFW	01FFFH
	DEFW	0007FH
	DEFW	007FFH
	DEFW	003FFH
	DEFW	0003FH
	DEFW	007FFH
	DEFW	0007FH
	DEFW	007FFH

J2297:	RST	10H
	LD	A,2CH	; ","
	CALL	C2318
	CP	14H	; 20 
	JR	NC,J22C2
	PUSH	HL
J22A2:	CP	02H	; 2 
	JR	NZ,J22B2
	LD	A,(LINLEN)
	CP	40+1
	JR	C,J22B2
	LD	HL,1000H
	JR	J22BD

J22B2:	LD	HL,TXTNAM
	LD	D,00H
	ADD	HL,DE
	ADD	HL,DE
	LD	A,(HL)
	INC	HL
	LD	H,(HL)
	LD	L,A
J22BD:	CALL	C05DE			; convert to SGN and put in DAC
	POP	HL
	RET

J22C2:	PUSH	HL
	SUB	19H
	JR	NC,J22D3
	ADD	A,0FH	; 15 
	LD	E,A
	CP	0DH	; 13 
	JR	NZ,J22A2
	LD	HL,(D22DE)
	JR	J22BD

J22D3:	LD	D,00H
	LD	E,A
	LD	HL,I22E0
	ADD	HL,DE
	LD	H,(HL)
	LD	L,D
	JR	J22BD

D22DE:	DEFW	01E00H

I22E0:	DEFB	000H,000H,000H,076H,078H,000H,000H,000H
	DEFB	076H,078H,000H,000H,000H,0FAH,0F0H,000H
	DEFB	000H,000H,0FAH,0F0H

J22F4:	CALL	C0602			; evaluate address operand
	PUSH	DE
	RST	08H
	DEFB	","
	RST	28H			; evaluate byte operand
	EX	(SP),HL
	CALL	C08CB			; write VRAM (active page)
	LD	HL,0
	CALL	C08D6			; read VRAM (active page)
	POP	HL
	RET

J2307:	CALL	C05B4			; convert address to integer
	CALL	C08D6			; read VRAM (active page)
	PUSH	AF
	LD	HL,0
	CALL	C08D6			; read VRAM (active page)
	POP	AF
	JP	C05E4			; byte to DAC


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2318:	PUSH	AF
	RST	08H
	DEFB	"("
	RST	28H			; evaluate byte operand
	POP	AF
	CP	E
	JP	C,J0546			; illegal function call error
	RST	08H
	DEFB	")"
	LD	A,E
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2325:	LD	A,(VALTYP)
	CP	08H	; 8 
	JR	NC,J2331
	SUB	03H	; 3 
	OR	A
	SCF
	RET

J2331:	SUB	03H	; 3 
	OR	A
	RET

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________
;	Remark	NOT USED

D2335:	CALL	C2325
	LD	HL,(DAC+2)
	RET	M
	JP	Z,J0552			; type mismatch error
	JP	J05F0			; convert float (SGN or DBL) to integer

J2342:	CP	0C5H
	JP	Z,J3676
	LD	(BUF),HL
	CP	28H	; "("
	JR	Z,J23C3
	CALL	C24F6
	LD	(BUF+4),DE
	LD	(BUF+6),BC
	JR	NC,J238C
	DEC	HL
	RST	10H
	JP	Z,J254D
	CP	0D9H
	JR	NZ,J236F
	RST	10H
	CP	28H	; "("
	JR	NZ,J2378
	XOR	A
	LD	(BUF+17),A
	JR	J2372

J236F:	CALL	C252B
J2372:	PUSH	HL
	LD	HL,J30A8		; BLTVD
	JR	J23A4

J2378:	CALL	C24F6
	JP	C,J254D
	LD	(BUF+8),DE
	LD	(BUF+10),BC
	PUSH	HL
	LD	HL,J306F		; BLTMD
	JR	J23BD

J238C:	DEC	HL
	RST	10H
	CP	0D9H
	JR	NZ,J239D
	RST	10H
	CP	28H	; "("
	JR	NZ,J23AB
	XOR	A
	LD	(BUF+17),A
	JR	J23A0

J239D:	CALL	C252B
J23A0:	PUSH	HL
	LD	HL,J2F42
J23A4:	LD	(BUF+2),HL		; BLTVM
	POP	HL
	JP	J24A9

J23AB:	CALL	C24F6
	JP	NC,J0540		; syntax error
	LD	(BUF+8),DE
	LD	(BUF+10),BC
	PUSH	HL
	LD	HL,J307C		; BLTDM
J23BD:	LD	(BUF+2),HL
	JP	J24E4

J23C3:	PUSH	HL
	LD	HL,J2EB9
	LD	(BUF+2),HL		; BLTVV
	POP	HL
	CALL	C059C			; evaluate complex graphic coordinatepair
	PUSH	BC
	PUSH	DE
	RST	08H
	DEFB	0F2H
	CALL	C05A2
	DEC	HL
	RST	10H
	CP	0D9H
	LD	A,(ACPAGE)
	JR	Z,J23E1
	RST	08H
	DEFB	","
	RST	28H			; evaluate byte operand
J23E1:	CALL	C1F93
	JP	C,J0546			; illegal function call error
	LD	(BUF+7),A
	POP	DE
	POP	BC
	RST	08H
	DEFB	0D9H
	PUSH	HL
	CALL	C12C4
	LD	(BUF+4),BC
	LD	A,E
	LD	(BUF+6),A
	CALL	C25F1
	CALL	C12C4
	CALL	C25F1
	EX	AF,AF'
	XOR	A
	EX	AF,AF'
	CALL	C25D5
	JR	NC,J240F
	EX	AF,AF'
	OR	04H	; 4 
	EX	AF,AF'
J240F:	INC	HL
	LD	(BUF+12),HL
	CALL	C25E6
	JR	NC,J241C
	EX	AF,AF'
	OR	08H	; 8 
	EX	AF,AF'
J241C:	INC	HL
	LD	(BUF+14),HL
	EX	AF,AF'
	LD	(BUF+17),A
	EX	AF,AF'
	PUSH	BC
	PUSH	DE
	LD	BC,(BUF+12)
	LD	E,L
	LD	D,H
	CALL	C3376
	JP	C,J0546			; illegal function call error
	POP	DE
	POP	BC
	POP	HL
	DEC	HL
	RST	10H
	CP	28H	; "("
	JR	Z,J24A9
	CALL	C24F6
	LD	(BUF+8),DE
	LD	(BUF+10),BC
	JR	NC,J2450
	PUSH	HL
	LD	HL,J31B4		; BLTDV
	JP	J24A4

J2450:	PUSH	HL
	PUSH	BC
	PUSH	DE
	LD	BC,(BUF+12)
	LD	DE,(BUF+14)
	CALL	C3052
	LD	A,00H
	ADD	A,A
	LD	E,A
	CALL	C253B
	DEC	B
	JR	Z,J248D
	DEC	B
	JR	Z,J2480
	LD	BC,3
	ADD	HL,BC
	LD	A,E
	ADC	A,00H
	SRL	A
	RR	H
	RR	L
	SRL	A
	RR	H
	RR	L
	JR	J248D

J2480:	LD	BC,1
	ADD	HL,BC
	LD	A,E
	ADC	A,00H
	SRL	A
	RR	H
	RR	L
J248D:	POP	DE
	ADD	HL,DE
	JR	C,J249E
	NOP
	LD	DE,4
	ADD	HL,DE
	JR	C,J249E
	NOP
	POP	DE
	EX	DE,HL
	AND	A
	SBC	HL,DE
J249E:	JP	C,J0546			; illegal function call error
	LD	HL,J2EDF		; BLTMV
J24A4:	LD	(BUF+2),HL
	JR	J24DE

J24A9:	CALL	C05A2			; evaluate simple graphic coordinatepair
	PUSH	HL
	CALL	C12C4
	LD	(BUF+8),BC
	LD	A,E
	LD	(BUF+10),A
	POP	HL
	XOR	A
	LD	(BUF+11),A
	DEC	HL
	RST	10H
	LD	A,(ACPAGE)
	JR	Z,J24CE
	RST	08H
	DEFB	","
	CP	","
	LD	A,(ACPAGE)
	CALL	NZ,C060E		; evaluate byte operand
J24CE:	CALL	C1F93
	JP	C,J0546			; illegal function call error
	LD	(BUF+11),A
	CALL	C2594
	LD	(BUF+18),A
	PUSH	HL
J24DE:	CALL	C062C			; new style screenmode ?
	JP	C,J0546			; nope, illegal function call error
J24E4:	LD	HL,I24F0
	PUSH	HL
	LD	HL,(BUF+2)
	PUSH	HL
	LD	HL,BUF+4
	RET

I24F0:	POP	HL
	JP	C,J0546			; illegal function call error
	AND	A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C24F6:	PUSH	HL
	CALL	C05FC			; evaluate expression
	LD	A,(VALTYP)
	CP	03H	; 3 
	JR	Z,J2523
	POP	HL
	LD	A,01H	; 1 
	LD	(SUBFLG),A
	CALL	C05D8			; locate variable
	JP	NZ,J0546		; illegal function call error
	LD	(SUBFLG),A
	PUSH	HL
	LD	H,B
	LD	L,C
	EX	DE,HL
	ADD	HL,DE
	DEC	HL
	PUSH	HL
	LD	A,(BC)
	ADD	A,A
	LD	L,A
	LD	H,00H
	INC	BC
	ADD	HL,BC
	EX	DE,HL
	POP	BC
	POP	HL
	AND	A
	RET

J2523:	PUSH	HL
	CALL	C0614			; free temporary string with type check
	POP	HL
	POP	DE
	SCF
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C252B:	RST	08H
	DEFB	","
	RST	28H			; evaluate byte operand
	CP	04H	; 4 
	JP	NC,J0546		; illegal function call error
	ADD	A,A
	ADD	A,A
	LD	(BUF+17),A
	RST	08H
	DEFB	0D9H
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C253B:	LD	B,01H	; 1 
	LD	A,(SCRMOD)
	CP	08H	; 8 
	RET	Z
	INC	B
	CP	05H	; 5 
	RET	Z
	CP	07H	; 7 
	RET	Z
	INC	B
	INC	B
	RET

J254D:	XOR	A
	LD	(SUBFLG),A
	LD	HL,(BUF)
	SCF
	RET

J2556:	PUSH	AF
	CALL	C05A2			; evaluate simple graphic coordinatepair
	POP	AF
	CALL	C2572
	CALL	C2594
	PUSH	HL
	CALL	C12C4
	JR	NC,J256D
	CALL	C1342
	CALL	C13B5
J256D:	POP	HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C256F:	LD	A,(FORCLR)

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2572:	PUSH	BC
	PUSH	DE
	LD	E,A
	CALL	C0620			; in textmode ?
	JP	C,J0546			; yep, illegal function call error
	DEC	HL
	RST	10H
	JR	Z,J2586
	RST	08H
	DEFB	","
	CP	","
	JR	Z,J2586
	RST	28H			; evaluate byte operand
J2586:	LD	A,E
	PUSH	HL
	CALL	C13AD
	JP	C,J0546			; illegal function call error
	POP	HL
	POP	DE
	POP	BC
	JP	J1D3E


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2594:	LD	A,00H
	PUSH	DE
	LD	D,00H
	LD	E,A
	DEC	HL
	RST	10H
	JR	Z,J25CC
	RST	08H
	DEFB	","
	JP	Z,J054C			; missing operand error
	CP	","
	JR	Z,J25CC
	LD	E,A
	CP	54H	; "T"
	JR	Z,J25C4
	CP	0D9H
	JR	Z,J25BC
	INC	A
	JR	NZ,J25CB
	RST	10H
	RST	08H
	DEFB	08DH
	RST	08H
	DEFB	"D"
	LD	E,0F6H
	JR	J25C1

J25BC:	RST	10H
	RST	08H
	DEFB	"R"
	LD	E,0F7H
J25C1:	DEC	HL
	JR	J25C9

J25C4:	RST	10H
	JP	Z,J0540			; syntax error
	LD	E,A
J25C9:	LD	D,08H	; 8 
J25CB:	RST	10H
J25CC:	LD	A,E
	CALL	C27B6
	JP	C,J0540			; syntax error
	POP	DE
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C25D5:	LD	HL,(GXPOS)
	AND	A
	SBC	HL,BC
J25DB:	RET	NC

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C25DC:	XOR	A
	SUB	L
	LD	L,A
	SBC	A,H
	SUB	L
	LD	H,A
	XOR	A
	SUB	01H	; 1 
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C25E6:	LD	HL,(GYPOS)
	AND	A
	SBC	HL,DE
	JR	J25DB


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C25EE:	CALL	C12C4

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C25F1:	CALL	C2600

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C25F4:	PUSH	HL
	PUSH	BC
	LD	HL,(GXPOS)
	EX	(SP),HL
	LD	(GXPOS),HL
	POP	BC
	POP	HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2600:	PUSH	HL
	LD	HL,(GYPOS)
	EX	DE,HL
	JR	J263A

J2607:	CALL	C059C			; evaluate complex graphic coordinatepair
	PUSH	BC
	PUSH	DE
	RST	08H
	DEFB	0F2H
	CALL	C05A2
	CALL	C256F
	POP	DE
	POP	BC
	DEC	HL
	RST	10H
	JR	Z,J263F
	RST	08H
	DEFB	","
	CP	","
	JR	Z,J263F
	RST	08H
	DEFB	"B"
	JR	Z,J2648
	CP	","
	JR	Z,J2648
	RST	08H
	DEFB	"F"
J262A:	CALL	C2594
	PUSH	HL
	CALL	C2898
J2631:	LD	HL,(GRPACX)
	LD	(GXPOS),HL
	LD	HL,(GRPACY)
J263A:	LD	(GYPOS),HL
	POP	HL
	RET

J263F:	CALL	C2594
	PUSH	HL
	CALL	C2823
	JR	J2631

J2648:	CALL	C2594
	PUSH	HL
	CALL	C27E7
	JR	J2631

J2651:	CALL	C059C			; evaluate complex graphic coordinatepair
	PUSH	BC
	PUSH	DE
	CALL	C256F
	LD	A,(ATRBYT)
	LD	E,A
	DEC	HL
	RST	10H
	JR	Z,J2664
	RST	08H
	DEFB	","
	RST	28H			; evaluate byte operand
J2664:	LD	A,E
	AND	A
	CALL	C14C7
	JP	C,J0546			; illegal function call error
	POP	DE
	POP	BC
	PUSH	HL
	CALL	C27AD
	CALL	C1342
	LD	DE,1
	CALL	C277E
	JR	Z,J2691
	PUSH	HL
	CALL	C278F
	POP	DE
	ADD	HL,DE
	EX	DE,HL
	XOR	A
	CALL	C275B
	LD	A,40H	; "@"
	CALL	C275B
	LD	B,0C0H
	JR	J26B3

J2691:	POP	HL
	RET

J2693:	LD	A,(INTFLG)
	OR	A
	CALL	NZ,C0589
	LD	A,(LOHDIR)
	OR	A
	JR	Z,J26AC
	LD	HL,(LOHADR)
	PUSH	HL
	LD	HL,(LOHMSK)
	PUSH	HL
	LD	HL,(LOHCNT)
	PUSH	HL
J26AC:	POP	DE
	POP	BC
	POP	HL
	LD	A,C
	CALL	C137D
J26B3:	LD	A,B
	LD	(PDIREC),A
	ADD	A,A
	JR	Z,J2691
	PUSH	DE
	JR	NC,J26C2
	CALL	C1414
	JR	J26C5

J26C2:	CALL	C13FC
J26C5:	POP	DE
	JR	C,J26AC
	CALL	C277E
	JP	Z,J26AC
	XOR	A
	LD	(LOHDIR),A
	CALL	C278F
	LD	E,L
	LD	D,H
	OR	A
	JR	Z,J26F4
	DEC	HL
	DEC	HL
	LD	A,H
	ADD	A,A
	JR	C,J26F4
	LD	(LOHCNT),DE
	CALL	C1376
	LD	(LOHADR),HL
	LD	(LOHMSK),A
	LD	A,(PDIREC)
	CPL
	LD	(LOHDIR),A
J26F4:	LD	HL,(MOVCNT)
	ADD	HL,DE
	EX	DE,HL
	CALL	C274F
	LD	HL,(CSAVEA)
	LD	A,(CSAVEM)
	CALL	C137D
J2705:	LD	HL,(SKPCNT)
	LD	DE,(MOVCNT)
	OR	A
	SBC	HL,DE
	JR	Z,J274C
	JR	C,J272F
	EX	DE,HL
	LD	B,01H	; 1 
	CALL	C277E
	JR	Z,J274C
	OR	A
	JR	Z,J2705
	EX	DE,HL
	LD	HL,(CSAVEA)
	LD	A,(CSAVEM)
	LD	C,A
	LD	A,(PDIREC)
	LD	B,A
	CALL	C2760
	JR	J2705

J272F:	CALL	C25DC
	DEC	HL
	DEC	HL
	LD	A,H
	ADD	A,A
	JR	C,J274C
	INC	HL
	PUSH	HL
	EX	DE,HL
	LD	HL,(CLOC)
	OR	A
	SBC	HL,DE
	LD	(CLOC),HL
	POP	DE
	LD	A,(PDIREC)
	CPL
	CALL	C275B
J274C:	JP	J2693


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C274F:	LD	A,(LFPROG)
	LD	C,A
	LD	A,(RTPROG)
	OR	C
	RET	Z
	LD	A,(PDIREC)

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C275B:	LD	B,A
	CALL	C1376
	LD	C,A

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2760:	EX	(SP),HL
	PUSH	BC
	PUSH	DE
	PUSH	HL
	LD	C,02H	; 2 
	PUSH	HL
	LD	HL,(STREND)
	LD	B,00H
	ADD	HL,BC
	ADD	HL,BC
	LD	A,88H
	SUB	L
	LD	L,A
	LD	A,0FFH
	SBC	A,H
	LD	H,A
	JR	C,J277B
	ADD	HL,SP
	POP	HL
	RET	C
J277B:	JP	J0558			; out of memory


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C277E:	CALL	C14DB
	LD	(SKPCNT),DE
	LD	(MOVCNT),HL
	LD	A,H
	OR	L
	LD	A,C
	LD	(RTPROG),A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C278F:	CALL	C1376
	PUSH	HL
	PUSH	AF
	LD	HL,(CSAVEA)
	LD	A,(CSAVEM)
	CALL	C137D
	POP	AF
	POP	HL
	LD	(CSAVEA),HL
	LD	(CSAVEM),A
	CALL	C158F
	LD	A,C
	LD	(LFPROG),A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C27AD:	PUSH	HL
	CALL	C12C4
	JP	NC,J0546		; illegal function call error
	POP	HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C27B6:
	AND	A
	JR	Z,J27E2
	PUSH	BC
	LD	B,A
	CALL	C062C			; new style screenmode ?
	LD	A,B
	POP	BC
	RET	C			; nope, quit
	CP	0F8H
	JR	Z,J27E0
	CP	0F7H
	JR	Z,J27DD
	CP	0F6H
	JR	Z,J27DA
	CP	0C3H
	JR	Z,J27D7
	CP	0C2H
	SCF
	RET	NZ
	XOR	A
	DEFB	0C2H
J27D7:
	LD	A,4
	DEFB	0C2H
J27DA:
	LD	A,1
	DEFB	0C2H
J27DD:
	LD	A,2
	DEFB	0C2H
J27E0:
	LD	A,3
J27E2:
	OR	D
	LD	(LOGOPR),A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C27E7:
	CALL	C25EE
	CALL	C25EE
	CALL	C25D5
	JR	Z,C2823
	CALL	C,C25F4
	CALL	C25E6
	JR	Z,C2823
	CALL	C,C2600
	LD	HL,(GYPOS)
	PUSH	HL
	PUSH	DE
	EX	DE,HL
	CALL	C2823
	POP	HL
	LD	(GYPOS),HL
	EX	DE,HL
	CALL	C2823
	POP	HL
	DEC	HL
	INC	DE
	LD	(GYPOS),HL
	LD	HL,(GXPOS)
	PUSH	BC
	LD	C,L
	LD	B,H
	CALL	C2823
	POP	HL
	LD	(GXPOS),HL
	LD	C,L
	LD	B,H

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2823:
	PUSH	BC
	PUSH	DE
	PUSH	HL
	LD	HL,(GYPOS)
	PUSH	HL
	LD	HL,(GXPOS)
	PUSH	HL
	CALL	C25EE
	CALL	C12C4
	CALL	C25E6
	CALL	C,C25F1
	PUSH	DE
	PUSH	HL
	EX	AF,AF'
	XOR	A
	EX	AF,AF'
	CALL	C25D5
	EX	DE,HL
	JR	NC,J2849
	EX	AF,AF'
	OR	04H	; 4 
	EX	AF,AF'
J2849:	POP	HL
	RST	20H
	JR	C,J2852
	EX	DE,HL
	EX	AF,AF'
	OR	01H	; 1 
	EX	AF,AF'
J2852:	EX	(SP),HL
	CALL	C2980			; wait for vdp command to complete
	LD	A,36
	DI
	OUT	(VDP_PORT+1),A
	LD	A,080H+17
	OUT	(VDP_PORT+1),A			; set indirect vdp register pointer to 36, autoincrement
	LD	A,C
	OUT	(VDP_PORT+3),A
	LD	A,B
	OUT	(VDP_PORT+3),A
	LD	A,L
	OUT	(VDP_PORT+3),A
	LD	A,(ACPAGE)
	OUT	(VDP_PORT+3),A
	LD	A,E
	OUT	(VDP_PORT+3),A
	LD	A,D
	OUT	(VDP_PORT+3),A
	POP	HL
	LD	A,L
	OUT	(VDP_PORT+3),A
	LD	A,H
	OUT	(VDP_PORT+3),A
	LD	A,(ATRBYT)
	OUT	(VDP_PORT+3),A
	EX	AF,AF'
	OUT	(VDP_PORT+3),A
	LD	A,(LOGOPR)
	AND	0FH	; 15 
	OR	70H	; "p"
	OUT	(VDP_PORT+3),A
	EI
	POP	HL
	LD	(GXPOS),HL
	POP	HL
	LD	(GYPOS),HL
	POP	HL
	POP	DE
	POP	BC
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2898:	CALL	C12C4
	LD	L,C
	LD	H,B
	LD	A,E
	CALL	C29A5			; write DX,DY registers
	CALL	C25F1
	CALL	C25EE
	EX	AF,AF'
	XOR	A
	EX	AF,AF'
	CALL	C25D5
	JR	NC,J28B3
	EX	AF,AF'
	OR	04H	; 4 
	EX	AF,AF'
J28B3:	INC	HL
	PUSH	HL
	CALL	C25E6
	JR	NC,J28BE
	EX	AF,AF'
	OR	08H	; 8 
	EX	AF,AF'
J28BE:	INC	HL
	POP	DE
	CALL	C29C4			; write NX,NY registers
	EX	AF,AF'
	CALL	C29DB			; write ARG register
	EX	AF,AF'
	LD	A,(ATRBYT)
	CALL	C29EB			; write colorregister
	LD	A,(LOGOPR)
	AND	0FH
	ADD	A,80H			; LMMV (logical move) command
	CALL	C29E3			; execute vdp command

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C28D8:
	LD	B,04H	; 4 
	JR	J28E6


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C28DC:
	LD	B,06H	; 6 
	JR	J28E6


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C28E0:
	LD	B,02H	; 2 
	JR	J28E6


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C28E4:
	LD	B,00H
J28E6:
	PUSH	BC
	CALL	C2925			; setup SX,SY and CLR
	POP	AF
	DI
	OUT	(VDP_PORT+1),A
	LD	A,080H+45
	OUT	(VDP_PORT+1),A			; setup ARG
	LD	A,60H			; SRCH command
	OUT	(VDP_PORT+1),A
	LD	A,080H+46
	OUT	(VDP_PORT+1),A			; execute command
	CALL	C2980			; wait for vdp command to complete
	AND	10H	; 16 
	RET	Z
	LD	A,8
	DI
	OUT	(VDP_PORT+1),A
	LD	A,080H+15
	OUT	(VDP_PORT+1),A			; select vdp statusregister 8
	IN	A,(VDP_PORT+1)
	LD	L,A
	LD	A,9
	OUT	(VDP_PORT+1),A
	LD	A,080H+15
	OUT	(VDP_PORT+1),A			; select vdp statusregister 9
	IN	A,(VDP_PORT+1)
	PUSH	AF
	XOR	A
	OUT	(VDP_PORT+1),A
	LD	A,080H+15
	OUT	(VDP_PORT+1),A			; select vdp statusregister 0
	EI
	POP	AF
	AND	01H	; 1 
	LD	H,A
	SCF
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2925:
	PUSH	AF
	CALL	C2980			; wait for vdp command to complete
	LD	A,32
	DI
	OUT	(VDP_PORT+1),A
	LD	A,080H+17
	OUT	(VDP_PORT+1),A			; set indirect vdp register pointer to 32, autoincrement
	LD	A,L
	OUT	(VDP_PORT+3),A			; SX low
	LD	A,H
	OUT	(VDP_PORT+3),A			; SX high
	LD	A,E
	OUT	(VDP_PORT+3),A			; SY low
	LD	A,(ACPAGE)
	OUT	(VDP_PORT+3),A			; SY high
	POP	AF
	OUT	(VDP_PORT+1),A
	LD	A,080H+44
	OUT	(VDP_PORT+1),A			; CLR (data to VDP)
	EI
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2949:
	PUSH	DE
	CALL	C29A0			; write SX,SY registers
	XOR	A
	CALL	C29DB			; write ARG register
	LD	A,40H			; POINT vdp command
	CALL	C29E3			; execute vdp command
	CALL	C2980			; wait for vdp command to complete
	LD	A,7
	CALL	C298B			; read vdp status register 7
	EI
	POP	DE
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2961:
	CALL	C29A5			; write DX,DY registers
	LD	A,44
	DI
	OUT	(VDP_PORT+1),A
	LD	A,080H+17
	OUT	(VDP_PORT+1),A			; set indirect vdp register pointer to 44, autoincrement
	LD	A,(ATRBYT)
	OUT	(VDP_PORT+3),A
	XOR	A
	OUT	(VDP_PORT+3),A
	LD	A,(LOGOPR)
	AND	0FH	; 15 
	OR	50H	; "P"
	OUT	(VDP_PORT+3),A
	EI
	RET

;	  Subroutine wait for vdp command to complete
;	     Inputs  ________________________
;	     Outputs ________________________

C2980:
	LD	A,2
	CALL	C298B			; read vdp status register 2
	EI
	RRCA				; vdp command executing ?
	JR	C,C2980			; yep, wait
	RLCA
	RET

;	  Subroutine read vdp status register
;	     Inputs  ________________________
;	     Outputs ________________________

C298B:
	DI
	OUT	(VDP_PORT+1),A
	LD	A,080H+15
	OUT	(VDP_PORT+1),A			; select vdp statusregister
	PUSH	HL
	POP	HL
	IN	A,(VDP_PORT+1)			; read vdp statusregister
	PUSH	AF
	XOR	A
	OUT	(VDP_PORT+1),A
	LD	A,080H+15
	OUT	(VDP_PORT+1),A			; select vdp statusregister 0
	POP	AF
	RET


;	  Subroutine write SX,SY registers
;	     Inputs  ________________________
;	     Outputs ________________________

C29A0:
	PUSH	AF
	LD	A,32
	JR	J29A8


;	  Subroutine write DX,DY registers
;	     Inputs  ________________________
;	     Outputs ________________________

C29A5:
	PUSH	AF
	LD	A,36
J29A8:
	PUSH	AF
	CALL	C2980			; wait for vdp command to complete
	DI
	POP	AF
	OUT	(VDP_PORT+1),A
	LD	A,080H+17
	OUT	(VDP_PORT+1),A			; set indirect vdp register pointer
	LD	A,L
	OUT	(VDP_PORT+3),A
	LD	A,H
	OUT	(VDP_PORT+3),A
	POP	AF
	OUT	(VDP_PORT+3),A
	LD	A,(ACPAGE)
	OUT	(VDP_PORT+3),A
	EI
	RET


;	  Subroutine write NX,NY registers
;	     Inputs  ________________________
;	     Outputs ________________________

C29C4:
	LD	A,40
	DI
	OUT	(VDP_PORT+1),A
	LD	A,080H+17
	OUT	(VDP_PORT+1),A			; set indirect vdp register pointer to 40, autoincrement
	LD	A,E
	OUT	(VDP_PORT+3),A
	LD	A,D
	OUT	(VDP_PORT+3),A
	LD	A,L
	OUT	(VDP_PORT+3),A
	LD	A,H
	OUT	(VDP_PORT+3),A
	EI
	RET


;	  Subroutine write ARG register
;	     Inputs  ________________________
;	     Outputs ________________________

C29DB:
	PUSH	AF
	DI
	OUT	(VDP_PORT+1),A
	LD	A,080H+45
	JR	J29F1


;	  Subroutine execute vdp command
;	     Inputs  ________________________
;	     Outputs ________________________

C29E3:
	PUSH	AF
	DI
	OUT	(VDP_PORT+1),A
	LD	A,080H+46
	JR	J29F1


;	  Subroutine write colorregister
;	     Inputs  ________________________
;	     Outputs ________________________

C29EB:
	PUSH	AF
	DI
	OUT	(VDP_PORT+1),A
	LD	A,080H+44
J29F1:	OUT	(VDP_PORT+1),A
	EI
	POP	AF
	RET

I29F6:
	DEFB	00H
	DEFB	20H
	DEFB	00H
	DEFB	00H
	DEFB	00H
	DEFB	00H
	DEFB	00H
	DEFB	00H
	DEFB	08H

	IF INTHZ = 60
	DEFB	00H
	ELSE
	DEFB	02H
	ENDIF
	
	DEFB	00H,00H,00H,00H,00H,00H
	DEFB	00H,00H,00H,00H,00H,3BH,05H,00H


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2A0E:									; splashscreen
	IF	CNTRY != 0
		call	RAM_COUNT					; by Fabio - chama rotina de calculo do tam. da RAM
		push	hl							; retorna em HL, salvar na pilha
	ENDIF
	CALL	C2A14
	JP	J1B6D


;	  Subroutine Display splashscreen
;	     Inputs  ________________________
;	     Outputs ________________________

C2A14:
	CALL	C2B40
	DI
	LD	A,55H
	LD	HL,0
	LD	BC,08000H
	CALL	C0977			; fill VRAM (active page)
	CALL	C2AFF
	LD	DE,0705H					; 05H -> R#7		; Set Text Color=0/Back drop color=5
	CALL	C2AC2			; write vdp register
	LD	DE,2D00H					; 00H -> R#45
	CALL	C2AC2			; write vdp register
	LD	D,2AH						; 00H -> R#42
	CALL	C2AC2			; write vdp register
	INC	D							; 00H -> R#43
	CALL	C2AC2			; write vdp register
	LD	D,27H						; 00H -> R#39
	CALL	C2AC2			; write vdp register
	LD	E, YDEST					; Y dest of logo image => 55 by default
	LD	HL,tab_logoData
	EXX
	LD	HL,tab_antiAliasing
	EXX
	LD	DE,0160H					; 60H -> R#01
	CALL	C2AC2			; write vdp register
	LD	A, LEFTBORDER				; pattern of left border?? => 0F0h by default
	LD	(BUF),A
J2A55:	DI
	CALL	C1034			; wait for VR to pass
	LD	A,(BUF)
	LD	B,A
	LD	C,23
	CALL	C0647			; write vdp register 23 (vertical scroll)
	LD	A,B
	INC	A
	LD	(BUF),A
	EI
	LD	A,03H	; 3 
	PUSH	AF
	LD	BC, XDEST					; X dest of logo image => 45 by default
	LD	D,26H	; "&"
	CALL	C2AC2			; write vdp register
	EXX
	LD	B,08H	; 8 

	IF ANTIALIASING = 1
		LD	C,(HL)
		INC	HL
	ELSE
		LD	C,0
	ENDIF

	EXX
J2A79:	CALL	C2AD8			; setup vdp dx,dy
	LD	A,(HL)
	INC	HL
	CP	0FEH
	JR	Z,J2AB9
	PUSH	HL
	PUSH	BC
	LD	BC, LOGOWIDTH				; Logo width => 422 by default
	JR	NC,J2A8C
	LD	B,0
	LD	C,A
J2A8C:	CALL	C2AD4			; setup vdp nx,ny
	POP	HL
	ADD	HL,BC
	LD	C,L
	LD	B,H
	POP	HL
	POP	AF
	XOR	03H
	CALL	C2AE3			; setup vdp color
	PUSH	AF
	CALL	C2AEC
	EXX
	SLA	C
	DJNZ	J2AA7
	LD	B,08H	; 8 
	LD	C,(HL)
	INC	HL
J2AA7:
	EXX
	JR	NC,J2A79
	DEC	BC
	CALL	C2AD8			; setup vdp dx,dy
	INC	BC
	LD	A,2
	CALL	C2AE3			; setup vdp color
	CALL	C2AFA
	JR	J2A79

J2AB9:
	INC	E
	POP	AF
	LD	A,(HL)
	CP	0FEH
	JR	NZ,J2A55
	EI
	RET


;	  Subroutine write vdp register
;	     Inputs  ________________________
;	     Outputs ________________________

C2AC2:
	PUSH	BC
	PUSH	AF
	DI
	LD	C,VDP_PORT+1
	OUT	(C),E
	LD	A,D
	OR	80H
	EX	(SP),HL
	EX	(SP),HL
	OUT	(C),A
	EI
	POP	AF
	POP	BC
	RET


;	  Subroutine setup vdp nx,ny
;	     Inputs  ________________________
;	     Outputs ________________________

C2AD4:
	LD	D,40
	JR	J2ADA


;	  Subroutine setup vdp dx,dy
;	     Inputs  ________________________
;	     Outputs ________________________

C2AD8:
	LD	D,36
J2ADA:
	PUSH	DE
	LD	E,C
	CALL	C2AC2			; write vdp register
	LD	E,B
	INC	D
	JR	J2AE7


;	  Subroutine setup vdp color
;	     Inputs  ________________________
;	     Outputs ________________________

C2AE3:
	PUSH	DE
	LD	E,A
	LD	D,44
J2AE7:
	CALL	C2AC2			; write vdp register
	POP	DE
	RET


;	  Subroutine 
;	     Inputs  ________________________
;	     Outputs ________________________

C2AEC:
	PUSH	DE
	LD	E,70H			; Line command
J2AEF:
	LD	D,46
	CALL	C2AC2			; write vdp register
	CALL	C2980			; wait for vdp command to complete
	DI
	POP	DE
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2AFA:
	PUSH	DE
	LD	E,50H			; Pset command
	JR	J2AEF


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2AFF:
	LD	A,2
	CALL	C1CF9			; select RP-501 bank 2
	LD	B,11
	CALL	READRTC			; read RP-501 register 11
	RLCA
	RLCA
	AND	0CH
	LD	C,A
	LD	B,00H
	LD	HL,I2B5F
	ADD	HL,BC
	LD	C,(HL)
	INC	HL
	LD	B,(HL)
	PUSH	BC
	INC	HL
	LD	E,(HL)
	INC	HL
	LD	D,(HL)
	EX	DE,HL
	LD	DE,1000H
	CALL	C2AC2			; write vdp register
	CALL	C2B33
	POP	HL
	CALL	C2B33
	LD	HL,0444H
	CALL	C2B33
	LD	HL,0777H

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2B33:
	PUSH	BC
	LD	C,VDP_PORT+2
	DI
	OUT	(C),L
	EX	(SP),HL
	EX	(SP),HL
	OUT	(C),H
	EI
	POP	BC
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2B40:
	LD	B,8
	LD	HL,I2B4F
J2B45:
	LD	D,(HL)
	INC	HL
	LD	E,(HL)
	INC	HL
	CALL	C2AC2			; write vdp register
	DJNZ	J2B45
	RET

I2B4F:						; tabela inicializacao registros VDP
	DEFB	00H,08H
	DEFB	01H,20H
	DEFB	08H,2AH

	IF INTHZ = 60
		DEFB	09H,00H
	ELSE
		DEFB	09H,02H
	ENDIF

	DEFB	02H,1FH
	DEFB	05H,0F7H
	DEFB	0BH,00H
	DEFB	06H,0EH

I2B5F:											; cores da tela de boot
	DEFB	07H,00H,00H,00H,20H,04H,27H,02H
	DEFB	72H,02H,56H,00H,70H,05H,70H,00H

	include	LOGO_FILE

;	  Subroutine BLTVV
;	     Inputs  ________________________
;	     Outputs ________________________

J2EB9:	LD	BC,(BUF+12)
	LD	DE,(BUF+14)
	CALL	C3376
	RET	C
	CALL	C2980			; wait for vdp command to complete
	LD	A,32			; register 32, autoincrement
	CALL	C336E 			; set indirect vdp register pointer
	LD	HL,BUF+4
	LD	BC,$0E00 | (VDP_PORT+3)
	OTIR
	LD	A,(HL)
	AND	0FH
	OR	90H			; LMMM (logical move) command
	OUT	(C),A
	EI
	AND	A
	RET

;	  Subroutine BLTMV
;	     Inputs  ________________________
;	     Outputs ________________________

J2EDF:	CALL	C3320
	LD	A,(SCRMOD)
	SUB	6
	JR	C,J2EFA
	JR	Z,J2F1B
	DEC	A
	JR	Z,J2EFA
J2EEE:	CALL	C3354
	LD	(HL),A
	INC	HL
	DEC	BC
	LD	A,C
	OR	B
	JP	NZ,J2EEE
	RET

J2EFA:	LD	D,02H	; 2 
	LD	(HL),00H
J2EFE:	CALL	C2F13
	CALL	C3354
	OR	(HL)
	LD	(HL),A
	DEC	BC
	LD	A,C
	OR	B
	JR	Z,J2F11
	DEC	D
	JR	NZ,J2EFE
	INC	HL
	JR	J2EFA

J2F11:	DEC	D
	RET	Z

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C2F13:	LD	A,(HL)
	ADD	A,A
	ADD	A,A
	ADD	A,A
	ADD	A,A
	LD	(HL),A
	AND	A
	RET

J2F1B:	LD	D,04H	; 4 
	LD	(HL),00H
J2F1F:	LD	A,(HL)
	ADD	A,A
	ADD	A,A
	LD	(HL),A
	CALL	C3354
	OR	(HL)
	LD	(HL),A
	DEC	BC
	LD	A,C
	OR	B
	JR	NZ,J2F31
	OR	E
	JR	Z,J2F37
	DEC	E
J2F31:	DEC	D
	JR	NZ,J2F1F
	INC	HL
	JR	J2F1B

J2F37:	DEC	D
	RET	Z
	LD	A,(HL)
J2F3A:	ADD	A,A
	ADD	A,A
	DEC	D
	JR	NZ,J2F3A
	LD	(HL),A
	AND	A
	RET

;	  Subroutine BLTVM
;	     Inputs  ________________________
;	     Outputs ________________________

J2F42:	CALL	C32C9
	RET	C
	CALL	C2980			; wait for vdp command to complete
	LD	A,32			; register 32, autoincrement
	CALL	C336E 			; set indirect vdp register pointer
	PUSH	BC
	LD	HL,BUF+4
	LD	BC,$0E00 | (VDP_PORT+3)
	OTIR
	POP	BC
	LD	A,(HL)
	AND	0FH
	OR	0B0H			; LMMC (logical move) command
	POP	HL
	PUSH	AF
	LD	A,(SCRMOD)
	SUB	06H	; 6 
	JR	C,J2F92
	JR	Z,J2FD4
	DEC	A
	JR	Z,J2F92
	CALL	C336C			; set indirect vdp register pointer to 44, non-autoincrement
	LD	A,(BUF+74)
	LD	E,A
	AND	A
	CALL	NZ,C33A2
	LD	A,(HL)
	OUT	(VDP_PORT+3),A
	POP	AF
	OUT	(VDP_PORT+1),A
	LD	A,080H+46
	OUT	(VDP_PORT+1),A			; execute command
	EI
J2F81:	DEC	BC
	LD	A,C
	OR	B
	RET	Z
	INC	HL
	LD	A,E
	AND	A
	CALL	NZ,C33A2
	JR	C,J2F81
	LD	A,(HL)
	OUT	(VDP_PORT+3),A
	JR	J2F81

J2F92:	CALL	C336C			; set indirect vdp register pointer to 44, non-autoincrement
	LD	D,0FH	; 15 
	LD	A,(BUF+74)
	LD	E,A
	AND	A
	CALL	NZ,C33A2
	LD	A,(HL)
	RRCA
	RRCA
	RRCA
	RRCA
	AND	D
	OUT	(VDP_PORT+3),A
	POP	AF
	OUT	(VDP_PORT+1),A
	LD	A,080H+46
	OUT	(VDP_PORT+1),A			; execute command
	EI
J2FAF:	DEC	BC
	LD	A,C
	OR	B
	RET	Z
	LD	A,E
	AND	A
	CALL	NZ,C33A2
	JR	C,J2FBE
	LD	A,(HL)
	AND	D
	OUT	(VDP_PORT+3),A
J2FBE:	INC	HL
	DEC	BC
	LD	A,C
	OR	B
	RET	Z
	LD	A,E
	AND	A
	CALL	NZ,C33A2
	JR	C,J2FAF
	LD	A,(HL)
	RRCA
	RRCA
	RRCA
	RRCA
	AND	D
	OUT	(VDP_PORT+3),A
	JR	J2FAF

J2FD4:	CALL	C336C			; set indirect vdp register pointer to 44, non-autoincrement
	LD	D,03H	; 3 
	LD	A,(BUF+74)
	AND	A
	CALL	NZ,C33A2
	LD	A,(HL)
	RRCA
	RRCA
	AND	D
	OUT	(VDP_PORT+3),A
	POP	AF
	OUT	(VDP_PORT+1),A
	LD	A,080H+46
	OUT	(VDP_PORT+1),A			; execute command
	EI
J2FEE:	DEC	BC
	LD	A,C
	OR	B
	JP	NZ,J2FF7
	OR	E
	RET	Z
	DEC	E
J2FF7:	LD	A,(BUF+74)
	AND	A
	CALL	NZ,C33A2
	JR	C,J3008
	LD	A,(HL)
	RRCA
	RRCA
	RRCA
	RRCA
	AND	D
	OUT	(VDP_PORT+3),A
J3008:	DEC	BC
	LD	A,C
	OR	B
	JP	NZ,J3011
	OR	E
	RET	Z
	DEC	E
J3011:	LD	A,(BUF+74)
	AND	A
	CALL	NZ,C33A2
	JR	C,J3020
	LD	A,(HL)
	RRCA
	RRCA
	AND	D
	OUT	(VDP_PORT+3),A
J3020:	DEC	BC
	LD	A,C
	OR	B
	JP	NZ,J3029
	OR	E
	RET	Z
	DEC	E
J3029:	LD	A,(BUF+74)
	AND	A
	CALL	NZ,C33A2
	JR	C,J3036
	LD	A,(HL)
	AND	D
	OUT	(VDP_PORT+3),A
J3036:	DEC	BC
	LD	A,C
	OR	B
	JP	NZ,J303F
	OR	E
	RET	Z
	DEC	E
J303F:	INC	HL
	LD	A,(BUF+74)
	AND	A
	CALL	NZ,C33A2
	JR	C,J2FEE
	LD	A,(HL)
	RLCA
	RLCA
	AND	D
	OUT	(VDP_PORT+3),A
	JP	J2FEE


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3052:	LD	HL,0
	LD	A,16
	AND	A
J3058:	PUSH	AF
	ADD	HL,HL
	JR	NC,J305F
	POP	AF
	SCF
	PUSH	AF
J305F:	EX	DE,HL
	ADD	HL,HL
	EX	DE,HL
	JR	NC,J306A
	ADD	HL,BC
	JR	NC,J306A
	POP	AF
	SCF
	PUSH	AF
J306A:	POP	AF
	DEC	A
	JR	NZ,J3058
	RET

;	  Subroutine BLTMD
;	     Inputs  ________________________
;	     Outputs ________________________

J306F:	PUSH	HL
	CALL	C32BB
	POP	HL
	INC	HL
	INC	HL
	INC	HL
	INC	HL
	LD	E,0			; open file
	JR	J3087

;	  Subroutine BLTDM
;	     Inputs  ________________________
;	     Outputs ________________________

J307C:	PUSH	HL
	INC	HL
	INC	HL
	INC	HL
	INC	HL
	CALL	C32BB
	POP	HL
	LD	E,1			; create file
J3087:	PUSH	DE
	LD	C,(HL)
	INC	HL
	LD	B,(HL)
	INC	HL
	LD	E,(HL)
	INC	HL
	LD	D,(HL)
	EX	DE,HL
	AND	A
	SBC	HL,BC
	INC	HL
	POP	DE
	PUSH	DE
	CALL	C33C7
	POP	DE
	DEC	E
	PUSH	AF
	CALL	NZ,C3444
	POP	AF
	CALL	Z,C3433
	CALL	C3457
	AND	A
	RET

;	  Subroutine BLTVD
;	     Inputs  ________________________
;	     Outputs ________________________

J30A8:	PUSH	HL
	CALL	C32BB
	LD	HL,0
	LD	E,L			; open file
	CALL	C33C7
	CALL	C3444
	LD	(BUF+23),HL
	LD	DE,(BUF+19)
	POP	HL
	LD	(HL),E
	INC	HL
	LD	(HL),D
	DEC	HL
	CALL	C32C9
	JR	C,J30F3
	CALL	C2980			; wait for vdp command to complete
	LD	A,32			; register 32, autoincrement
	CALL	C336E 			; set indirect vdp register pointer
	PUSH	BC
	LD	HL,BUF+4
	LD	BC,$0E00 | (VDP_PORT+3)
	OTIR
	POP	BC
	LD	A,(HL)
	AND	0FH
	OR	0B0H			; LMMC (logical move) command
	LD	HL,I30F2
	EX	(SP),HL
	PUSH	AF
	LD	A,(SCRMOD)
	SUB	6
	JR	C,J311C
	JP	Z,J315C
	DEC	A
	JR	Z,J311C
	JR	J30F9

I30F2:	AND	A
J30F3:	PUSH	AF
	CALL	C3457
	POP	AF
	RET

J30F9:	CALL	C336C			; set indirect vdp register pointer to 44, non-autoincrement
	CALL	C339D
	LD	A,(HL)
	OUT	(VDP_PORT+3),A
	POP	AF
	OUT	(VDP_PORT+1),A
	LD	A,080H+46
	OUT	(VDP_PORT+1),A			; execute command
	EI
J310A:	DEC	BC
	LD	A,C
	OR	B
	RET	Z
	INC	HL
	CALL	C3279
	CALL	C339D
	JR	C,J310A
	LD	A,(HL)
	OUT	(VDP_PORT+3),A
	JR	J310A

J311C:	CALL	C336C			; set indirect vdp register pointer to 44, non-autoincrement
	LD	D,00H
	CALL	C339D
	LD	A,(HL)
	RRCA
	RRCA
	RRCA
	RRCA
	AND	0FH
	OUT	(VDP_PORT+3),A
	POP	AF
	OUT	(VDP_PORT+1),A
	LD	A,080H+46
	OUT	(VDP_PORT+1),A			; execute command
	EI
J3135:	INC	D
	DEC	BC
	LD	A,C
	OR	B
	JR	NZ,J313E
	OR	E
	RET	Z
	DEC	E
J313E:	CALL	C3279
	LD	A,D
	RRCA
	LD	A,(HL)
	INC	HL
	JR	C,J314C
	DEC	HL
	RRCA
	RRCA
	RRCA
	RRCA
J314C:	PUSH	AF
	CALL	C339D
	JR	C,J3159
	POP	AF
	AND	0FH	; 15 
	OUT	(VDP_PORT+3),A
	JR	J3135

J3159:	POP	AF
	JR	J3135

J315C:	CALL	C336C			; set indirect vdp register pointer to 44, non-autoincrement
	LD	D,00H
	CALL	C339D
	LD	A,(HL)
	RRCA
	RRCA
	AND	03H	; 3 
	OUT	(VDP_PORT+3),A
	POP	AF
	OUT	(VDP_PORT+1),A
	LD	A,080H+46
	OUT	(VDP_PORT+1),A			; execute command
	EI
J3173:	INC	D
	DEC	BC
	LD	A,C
	OR	B
	JR	NZ,J317C
	OR	E
	RET	Z
	DEC	E
J317C:	CALL	C3279
	LD	A,D
	AND	03H	; 3 
	JR	Z,J3197
	DEC	A
	JR	Z,J319F
	DEC	A
	JR	Z,J319A
	CALL	C339D
	JR	C,J3194
	LD	A,(HL)
	AND	03H	; 3 
	OUT	(VDP_PORT+3),A
J3194:	INC	HL
	JR	J3173

J3197:	LD	A,(HL)
	JR	J31A2

J319A:	LD	A,(HL)
	RRCA
	RRCA
	JR	J31A4

J319F:	LD	A,(HL)
	RLCA
	RLCA
J31A2:	RLCA
	RLCA
J31A4:	PUSH	AF
	CALL	C339D
	JR	C,J31B1
	POP	AF
	AND	03H	; 3 
	OUT	(VDP_PORT+3),A
	JR	J3173

J31B1:	POP	AF
	JR	J3173

;	  Subroutine BLTDV
;	     Inputs  ________________________
;	     Outputs ________________________

J31B4:	PUSH	HL
	INC	HL
	INC	HL
	INC	HL
	INC	HL
	PUSH	HL
	CALL	C32BB
	LD	HL,0
	LD	E,1			; create file
	CALL	C33C7
	LD	DE,(BUF+19)
	POP	HL
	LD	(HL),E
	INC	HL
	LD	(HL),D
	POP	HL
	CALL	C3320
	LD	A,(SCRMOD)
	SUB	06H	; 6 
	JR	C,J31ED
	JR	Z,J3227
	DEC	A
	JR	Z,J31ED
J31DD:	CALL	C3292
	CALL	C3354
	LD	(HL),A
	INC	HL
	DEC	BC
	LD	A,C
	OR	B
	JR	NZ,J31DD
	JP	J3270

J31ED:	LD	D,00H
J31EF:	CALL	C3292
	LD	A,7
	DI
	OUT	(VDP_PORT+1),A
	LD	A,080H+15
	OUT	(VDP_PORT+1),A			; select vdp statusregister 7
	PUSH	HL
	POP	HL
	LD	A,D
	RRCA
	IN	A,(VDP_PORT+1)
	JR	NC,J3208
	OR	(HL)
	LD	(HL),A
	INC	HL
	JR	J320D

J3208:	ADD	A,A
	ADD	A,A
	ADD	A,A
	ADD	A,A
	LD	(HL),A
J320D:	XOR	A
	OUT	(VDP_PORT+1),A
	LD	A,080H+15
	OUT	(VDP_PORT+1),A			; select vdp statusregister 0
	EI
	INC	D
	DEC	BC
	LD	A,C
	OR	B
	JR	NZ,J31EF
	OR	E
	JR	Z,J3221
	DEC	E
	JR	J31EF

J3221:	LD	A,D
	RRCA
	JR	C,J3271
	JR	J3270

J3227:	LD	D,00H
J3229:	CALL	C3292
	LD	A,7
	DI
	OUT	(VDP_PORT+1),A
	LD	A,080H+15
	OUT	(VDP_PORT+1),A			; select vdp statusregister 7
	PUSH	HL
	POP	HL
	LD	A,D
	AND	03H	; 3 
	JR	Z,J3249
	DEC	A
	JR	Z,J324F
	DEC	A
	IN	A,(VDP_PORT+1)
	JR	Z,J3253
	OR	(HL)
	LD	(HL),A
	INC	HL
	JR	J3257

J3249:	IN	A,(VDP_PORT+1)
	RRCA
	RRCA
	JR	J3256

J324F:	IN	A,(VDP_PORT+1)
	RLCA
	RLCA
J3253:	RLCA
	RLCA
	OR	(HL)
J3256:	LD	(HL),A
J3257:	XOR	A
	OUT	(VDP_PORT+1),A
	LD	A,080H+15
	OUT	(VDP_PORT+1),A			; select vdp statusregister 0
	EI
	INC	D
	DEC	BC
	LD	A,C
	OR	B
	JR	NZ,J3229
	OR	E
	JR	Z,J326B
	DEC	E
	JR	J3229

J326B:	LD	A,D
	AND	03H	; 3 
	JR	NZ,J3271
J3270:	DEC	HL
J3271:	CALL	C32B0
	CALL	C3457
	AND	A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3279:	PUSH	DE
	PUSH	HL
	LD	HL,(BUF+19)
	LD	DE,(BUF+23)
	ADD	HL,DE
	EX	DE,HL
	POP	HL
	RST	20H
	POP	DE
	RET	C
	PUSH	BC
	PUSH	DE
	CALL	C3444
	LD	(BUF+23),HL
	JR	J32AA


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3292:	PUSH	DE
	PUSH	HL
	LD	HL,(BUF+19)
	LD	DE,(BUF+21)
	ADD	HL,DE
	EX	DE,HL
	POP	HL
	RST	20H
	POP	DE
	RET	C
	LD	HL,(BUF+21)
	PUSH	BC
	PUSH	DE
	CALL	C3436
	AND	A
J32AA:	POP	DE
	POP	BC
	LD	HL,(BUF+19)
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C32B0:	LD	DE,(BUF+19)
	AND	A
	SBC	HL,DE
	INC	HL
	JP	C3436


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C32BB:	LD	A,(HL)
	INC	HL
	LD	H,(HL)
	LD	L,A
	CALL	C05BA			; evaluate filespecification
	LD	A,D
	CP	09H	; 9 
	RET	C
	JP	J052E			; error 56


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C32C9:	LD	HL,(BUF+4)
	LD	C,(HL)
	INC	HL
	LD	B,(HL)
	INC	HL
	LD	E,(HL)
	INC	HL
	LD	D,(HL)
	INC	HL
	CALL	C3376
	RET	C
	PUSH	HL
	PUSH	BC
	PUSH	DE
	LD	HL,(BUF+8)
	LD	A,(BUF+17)
	AND	04H	; 4 
	JR	NZ,J32ED
	EX	DE,HL
	CALL	C3390
	AND	A
	SBC	HL,DE
	DEFB	03EH
J32ED:	INC	HL
	LD	(BUF+66),HL
	LD	(BUF+68),HL
	LD	D,B
	LD	E,C
	EX	DE,HL
	SBC	HL,DE
	JR	NC,J32FE
	LD	HL,0
J32FE:	LD	(BUF+70),HL
	LD	(BUF+72),HL
	LD	A,H
	OR	L
	LD	(BUF+74),A
	POP	DE
	POP	BC
	LD	(BUF+12),BC
	LD	(BUF+14),DE
	CALL	C3052
	LD	B,H
	LD	C,L
	LD	A,00H
	ADC	A,A
	LD	E,A
	AND	A
	POP	HL
	EX	(SP),HL
	JP	(HL)


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3320:	CALL	C2980			; wait for vdp command to complete
	LD	A,32			; register 32, autoincrement
	CALL	C336E 			; set indirect vdp register pointer
	LD	HL,BUF+4
	LD	BC,$0E00 | (VDP_PORT+3)
	OTIR
	LD	A,0A0H			; LMCM (logical move) command
	OUT	(C),A
	EI
	LD	BC,(BUF+12)
	LD	DE,(BUF+14)
	LD	HL,(BUF+8)
	LD	(HL),C
	INC	HL
	LD	(HL),B
	INC	HL
	LD	(HL),E
	INC	HL
	LD	(HL),D
	INC	HL
	PUSH	HL
	CALL	C3052
	LD	B,H
	LD	C,L
	LD	A,00H
	ADC	A,A
	LD	E,A
	POP	HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3354:	DI
	LD	A,7
	OUT	(VDP_PORT+1),A
	LD	A,080H+15
	OUT	(VDP_PORT+1),A			; select vdp statusregister 7
	PUSH	HL
	POP	HL
	IN	A,(VDP_PORT+1)
	PUSH	AF
	XOR	A
	OUT	(VDP_PORT+1),A
	LD	A,080H+15
	OUT	(VDP_PORT+1),A			; select vdp statusregister 0
	POP	AF
	EI
	RET


;	  Subroutine set indirect vdp register pointer to 44, non-autoincrement
;	     Inputs  ________________________
;	     Outputs ________________________

C336C:	LD	A,080H+44		; register 44, non-autoincrement

;	  Subroutine set indirect vdp register pointer
;	     Inputs  ________________________
;	     Outputs ________________________

C336E:	DI
	OUT	(VDP_PORT+1),A
	LD	A,080H+17
	OUT	(VDP_PORT+1),A			; set indirect vdp register pointer
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3376:	LD	A,B
	OR	C
	SCF
	RET	Z
	LD	A,D
	OR	E
	SCF
	RET	Z
	PUSH	HL
	PUSH	DE
	LD	E,C
	LD	D,B
	CALL	C3390
	RST	20H
	POP	DE
	POP	HL
	RET	C
	PUSH	HL
	LD	HL,212
	RST	20H
	POP	HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3390:	LD	HL,256
	LD	A,(SCRMOD)
	AND	06H	; 6 
	CP	06H	; 6 
	RET	NZ
	INC	H
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C339D:	LD	A,(BUF+74)
	AND	A
	RET	Z

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C33A2:	PUSH	HL
	LD	HL,(BUF+68)
	LD	A,L
	OR	H
	JR	Z,J33B0
	DEC	HL
	LD	(BUF+68),HL
	POP	HL
	RET

J33B0:	LD	HL,(BUF+72)
	DEC	HL
	LD	A,L
	OR	H
	JR	NZ,J33C1
	LD	HL,(BUF+66)
	LD	(BUF+68),HL
	LD	HL,(BUF+70)
J33C1:	LD	(BUF+72),HL
	POP	HL
	SCF
	RET

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C33C7:	LD	A,(H.PHYD+0)		; disk system available ?
	CP	0C9H
	JP	Z,J0546			; nope, illegal function call error
	PUSH	BC
	PUSH	HL
	LD	HL,BUF+25
	PUSH	HL
	LD	(HL),D			; driveid
	LD	A,E
	AND	A
	PUSH	AF
	EX	DE,HL
	INC	DE
	LD	HL,FILNAM
	LD	BC,11
	LDIR				; filename
	XOR	A
	LD	B,37-11-1
J33E6:	LD	(DE),A
	INC	DE
	DJNZ	J33E6			; clear rest of the FCB
	POP	AF
	POP	DE
	PUSH	AF
	LD	C,0FH			; _FOPEN (open file)
	JR	Z,J33F3
	LD	C,16H			; _FMAKE (create file)
J33F3:	CALL	C345C			; BDOS
	INC	A			; error ?
	JR	NZ,J3401		; nop, continue
	POP	AF
	JP	Z,J055E			; error 53
	LD	E,43H	; "C"
	JR	J3454

J3401:	LD	HL,1
	LD	(BUF+39),HL
	POP	AF
	POP	HL
	POP	DE
	LD	A,H
	OR	L
	JR	NZ,J3428
	LD	HL,-512
	ADD	HL,SP
	JR	NC,J3421
	LD	DE,(STREND)
	AND	A
	SBC	HL,DE
	JR	C,J3421
	LD	A,H
	AND	A
	JR	NZ,J3428
J3421:	LD	DE,(NULBUF)
	LD	HL,256
J3428:	LD	(BUF+19),DE
	LD	(BUF+21),HL
	LD	C,1AH			; _SETDTA
	JR	C345C			; BDOS


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3433:	LD	HL,(BUF+21)

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3436:	LD	DE,BUF+25
	LD	C,26H			; _WRBLK
	CALL	C345C			; BDOS
	AND	A
	RET	Z
	LD	E,42H
	JR	J3454


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3444:	LD	HL,(BUF+21)
	LD	DE,BUF+25
	LD	C,27H			; _RDBLK
	CALL	C345C			; BDOS
	LD	A,L
	OR	H
	RET	NZ
	LD	E,37H
J3454:	JP	J05C6			; BASIC error


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3457:
	LD	DE,BUF+25
	LD	C,10H			; _FCLOSE (close file)
					; BDOS

;	  Subroutine Do BDOS call with MAIN-ROM active
;	     Inputs  ________________________
;	     Outputs ________________________

C345C:
	LD	IX,C7FFD
	JP	C0262


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3463:
	LD	B,00H
	AND	7FH
	LD	C,A
	SBC	HL,BC
	RET

;	  Subroutine read lightpen status
;	     Inputs  ________________________
;	     Outputs ________________________

J346B:	IN	A,(LIGPEN_PORT+2)
	CPL
	AND	08H
	RET	Z
	LD	A,0FFH			; switch pressed
	RET

;	  Subroutine read lightpen
;	     Inputs  ________________________
;	     Outputs ________________________

J3474:	LD	HL,XSAVE+1
	XOR	A			; assume invalid data
	BIT	7,(HL)
	RET	Z
	RES	7,(HL)
	IN	A,(LIGPEN_PORT+2)
	AND	07H	; 7 
	LD	D,A
	IN	A,(LIGPEN_PORT)
	LD	L,A
	LD	H,00H
	IN	A,(LIGPEN_PORT+1)
	SRL	D
	RRA
	LD	E,A
	RL	H
	PUSH	HL
	PUSH	DE
	LD	BC,-3
	ADD	HL,BC
	EX	DE,HL
	LD	C,0EEH
	ADD	HL,BC
	LD	A,E
	OUT	(LIGPEN_PORT),A
	RR	D
	ADC	HL,HL
	LD	A,L
	OUT	(LIGPEN_PORT+1),A
	LD	A,H
	OUT	(LIGPEN_PORT+2),A
	POP	DE
	POP	HL
	IN	A,(LIGPEN_PORT+2)
	AND	20H
	RET	Z			; nope, quit with invalid data
	LD	A,(YSAVE+1)
	CALL	C3463
	LD	BC,212-1
	CALL	C34C7
	LD	A,L
	LD	(YSAVE),A
	EX	DE,HL
	LD	A,(XSAVE+1)
	CALL	C3463
	LD	BC,255

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C34C7:	LD	A,H
	RLA
	JR	C,J34D4
	PUSH	HL
	SBC	HL,BC
	POP	HL
	JR	C,J34D6
	LD	L,C
	LD	H,B
	DEFB	0DAH
J34D4:	LD	L,0
J34D6:	LD	A,L
	LD	(XSAVE),A
	LD	A,0FFH			; flag valid data
	RET

;	Subroutine	S.NEWPAD
;	Inputs		________________________
;	Outputs		________________________

J34DD:	CP	8
	JR	Z,J3474			; yep, read lightpen (function 0)
	CP	11
	JR	Z,J346B			; yep, read lightpen status (function 3)
	LD	E,0FH
	LD	BC,000BFH
	LD	HL,010EFH
	CP	12
	JR	Z,J3509			; read mouse/trackball port A (function 0)
	LD	BC,040FFH
	LD	HL,020DFH
	CP	16
	JR	Z,J3509			; read mouse/trackball port B (function 0)
	AND	03H			; functionnumber
	SUB	2			; function 2 ?
	LD	A,(XSAVE)
	RET	M			; function 1, return X value
	LD	A,(YSAVE)
	RET	Z			; function 2, return Y value
	XOR	A			; function 3, return status (OK)
	RET

;	  Subroutine read mouse/trackball
;	     Inputs  ________________________
;	     Outputs ________________________

J3509:	DI
	CALL	C357F			; pulse low
	CALL	C359A			; read 4 inputbits (used in case of a trackball ?)
	PUSH	AF
	CALL	C3585			; pulse high
	CALL	C3591			; wait ? and read 4 inputbits (DX7-DX4)
	PUSH	AF
	CALL	C357F			; pulse low
	CALL	C3597			; wait ? and read 4 inputbits (DX3-DX0)
	PUSH	AF
	CALL	C3585			; pulse high
	CALL	C3597			; wait ? and read 4 inputbits (DY7-DY4)
	PUSH	AF
	CALL	C357F			; pulse low
	CALL	C3597			; wait ? and read 4 inputbits (DY3-DY0)
	PUSH	AF

	IFNDEF	FS-5500
	CALL	C3585			; pulse high
	ELSE
	CALL	C3F8B
	ENDIF

	CALL	C3591			; wait ? and read 4 inputbits (DX7-DX4 for the second time)
	PUSH	AF
	CALL	C357F			; pulse low
	CALL	C35AA			; wait ? (DX3-DX0 are not read)
	CALL	C3585			; pulse high
	CALL	C35AA			; wait ? (DY7-DY4 are not read)
	CALL	C357F			; pulse low
	EI
	POP	AF
	POP	HL
	POP	DE
	POP	BC
	XOR	08H
	SUB	02H
	CP	0DH
	JR	C,J355D
	POP	AF
	CALL	C3577
	LD	(YSAVE),A
	POP	AF
	CALL	C3577
	JR	J3571

J355D:	LD	A,D
	ADD	A,A
	ADD	A,A
	ADD	A,A
	ADD	A,A
	OR	H
	NEG
	LD	(YSAVE),A
	POP	AF
	POP	DE
	ADD	A,A
	ADD	A,A
	ADD	A,A
	ADD	A,A
	OR	B
	NEG
J3571:	LD	(XSAVE),A
	LD	A,0FFH
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3577:	XOR	08H
	BIT	3,A
	RET	Z
	OR	0F0H
	RET


;	  Subroutine pulse low
;	     Inputs  ________________________
;	     Outputs ________________________

C357F:	CALL	C35A2
	AND	L
	JR	J3589


;	  Subroutine pulse high
;	     Inputs  ________________________
;	     Outputs ________________________

C3585:	CALL	C35A2
	OR	H
J3589:	PUSH	AF
	LD	A,E
	OUT	(PSG_PORT),A
	POP	AF
	OUT	(PSG_PORT+1),A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3591:	CALL	C35AA
	CALL	C35AA

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3597:	CALL	C35AA

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C359A:	LD	A,0EH
	CALL	C35A3
	AND	0FH
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C35A2:	LD	A,E

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C35A3:	OUT	(PSG_PORT),A
	IN	A,(PSG_PORT+2)
	AND	C
	OR	B
	RET

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C35AA:
	IFNDEF	FS-5500
	EX	(SP),HL
	EX	(SP),HL
	EX	(SP),HL
	EX	(SP),HL
	ELSE
	NOP
	NOP
	NOP
	NOP
	ENDIF

	RET

;	  Subroutine SET VIDEO statement
;	     Inputs  ________________________
;	     Outputs ________________________

C35AF:	CP	","
	JR	Z,J35EF
	RST	28H			; evaluate byte operand
	CP	4
	JR	NC,J3635
	AND	A
	JR	Z,J35C2
	CP	1
	JR	Z,J35C2
	LD	E,0
	DEC	A
J35C2:	PUSH	DE
	RRCA
	RRCA
	LD	D,A
	RRCA
	RRCA
	LD	E,A
	LD	A,(RG9SAV)
	AND	0CFH
	OR	E
	LD	C,9
	CALL	C36B4			; write vdp register 9
	LD	A,D
	XOR	0C0H
	LD	E,A			; b7,b6
	LD	A,3FH			; leave b5-b0
	CALL	C3661
	POP	DE
	LD	A,E
	RRCA
	RRCA
	RRCA
	LD	E,A
	LD	A,(RG8SAV)
	AND	0DFH
	OR	E
	CALL	C36B2			; write vdp register 8
	DEC	HL
	RST	10H
	RET	Z
J35EF:	RST	08H
	DEFB	","
	CP	","
	JR	Z,J3600
	CALL	C366C			; evaluate intensity parameter
	LD	A,0DFH			; leave b7,b6, b4-b0
	CALL	C3661
	DEC	HL
	RST	10H
	RET	Z
J3600:	RST	08H
	DEFB	","
	CP	","
	JR	Z,J3617
	CALL	C366C			; evaluate color direction parameter
	RRC	E
	LD	A,(RG8SAV)
	AND	0EFH
	OR	E
	CALL	C36B2			; write vdp register 8
	DEC	HL
	RST	10H
	RET	Z
J3617:	RST	08H
	DEFB	","
	CP	","
	JR	Z,J362C
	CALL	C366C			; evaluate synchronization parameter
	RLCA
	RLCA
	RLCA
	LD	E,A			; b3
	LD	A,0F7H			; leave b7-b4, b2-b0
	CALL	C3661
	DEC	HL
	RST	10H
	RET	Z
J362C:	RST	08H
	DEFB	","
	CP	","
	JR	Z,J3644

	IF	CNTRY = 0
	RST	28H			; evaluate byte operand
	CP	4
	ELSE
	CALL	C3CCD
	ENDIF

J3635:	JP	NC,J0546		; illegal function call error
	CPL
	AND	03H
	LD	E,A			; b1, b0
	LD	A,0FCH			; leave b7-b2
	CALL	C3661
	DEC	HL
	RST	10H
	RET	Z
J3644:	RST	08H
	DEFB	","
	CP	","
	JR	Z,J3658
	CALL	C366C			; evaluate external videoinput parameter
	RLCA
	RLCA
	LD	E,A			; b2
	LD	A,0FBH			; leave b7-b3, b1-b0
	CALL	C3661
	DEC	HL
	RST	10H
	RET	Z
J3658:	RST	08H
	DEFB	","
	CALL	C366C			; evaluate external videooutput parameter
	RRC	E			; b4
	LD	A,0EFH			; leave b7-b5, b3-b0

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3661:	PUSH	HL
	LD	HL,AVCSAV
	AND	(HL)
	OR	E
	LD	(HL),A
	OUT	(AV_PORT),A
	POP	HL
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C366C:	RST	28H			; evaluate byte operand
	CP	2
	JR	NC,J3635
	AND	A
	RET	Z
	LD	E,20H
	RET

J3676:	RST	10H
	JR	Z,J3686
	CP	","
	JR	Z,J3686
	CALL	C366C
	DEC	A
	JR	NZ,J3686
	LD	E,0
	DEFB	1
J3686:	LD	E,2
	DEC	HL
	RST	10H
	LD	A,0FFH
	JR	Z,J3693
	PUSH	DE
	RST	08H
	DEFB	","
	RST	28H			; evaluate byte operand
	POP	DE
J3693:	LD	D,A
	LD	A,(RG7SAV)
	PUSH	AF			; save vdp register 7
	LD	A,D
	CALL	C36AE			; write vdp register 7
	CALL	C36B8
	OR	40H
	CALL	C36B4			; write vdp register 0
	CALL	C36B8
	EI
	AND	0BFH
	CALL	C36B4			; write vdp register 0
	POP	AF
					; restore vdp register 7 and quit

;	  Subroutine write vdp register 7
;	     Inputs  ________________________
;	     Outputs ________________________

C36AE:	LD	C,7
	JR	C36B4			; write vdp register 7


;	  Subroutine write vdp register 8
;	     Inputs  ________________________
;	     Outputs ________________________

C36B2:	LD	C,8

;	  Subroutine write vdp register
;	     Inputs  ________________________
;	     Outputs ________________________

C36B4:	LD	B,A
	JP	C0647			; write vdp register


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C36B8:	LD	A,2
	CALL	C298B			; read vdp status register 2
	AND	40H			; in VR ?
	JR	Z,C36B8			; nope, wait
J36C1:	LD	A,2
	CALL	C298B			; read vdp status register 2
	BIT	6,A			; VR passed ?
	JR	NZ,J36C1		; nope, wait
	OR	E
	AND	02H
	JR	Z,C36B8
	LD	A,(RG0SAV)
	LD	C,0			; vdp register 0
	RET

;	  Subroutine MSX-BASIC device handler
;	     Inputs  ________________________
;	     Outputs ________________________

C36D5:	EI
	INC	A			; device inquiry ?
	JR	NZ,J36DF		; nope, handle MEM device operation
J36D9:	CALL	C392C			; devicename recognized ?
	RET	Z			; yep, quit
	SCF
	RET				; flag devicename not recognized

J36DF:	PUSH	HL
	LD	HL,I36EE-1
	ADD	A,L
	LD	L,A
	JR	NC,J36E8
	INC	H
J36E8:	LD	A,(HL)
	INC	HL
	LD	H,(HL)
	LD	L,A
	EX	(SP),HL
	RET

I36EE:	DEFW	C3973			; open
	DEFW	C39FA			; close
	DEFW	C38B9			; random
	DEFW	C3A1E			; seq output
	DEFW	C3A7C			; seq input
	DEFW	C3898			; loc
	DEFW	C38A3			; lof
	DEFW	C38B6			; eof
	DEFW	C38D7			; fpos
	DEFW	C3AC8			; putback

;	  Subroutine CALL MEMINI statement
;	     Inputs  ________________________
;	     Outputs ________________________

C3702:	DEC	HL
	RST	10H
	LD	DE,07FFFH
	JR	Z,J3714
	RST	08H
	DEFB	"("
	CALL	C0608
	JP	M,J0546
	RST	08H
	DEFB	")"
	RET	NZ
J3714:	PUSH	HL
	PUSH	DE
	LD	A,(SLTWRK+0)
	AND	A
	JR	Z,J3722
	BIT	5,A
	JR	NZ,J3777		; illegal function call error
	JR	J377A

J3722:	LD	HL,EXPTBL
	LD	BC,0400H
	XOR	A
J3729:	AND	03H
	OR	(HL)
J372C:	PUSH	BC
	PUSH	HL
	LD	H,C			; 00XXH
J372F:	LD	L,10H
J3731:	PUSH	AF
	CALL	C057D			; call RDSLT mainrom
	CPL
	LD	E,A			; inverted value
	POP	AF
	PUSH	DE
	PUSH	AF
	CALL	C0583			; call WRSLT mainrom
	POP	AF
	POP	DE
	PUSH	AF
	PUSH	DE
	CALL	C057D			; call RDSLT mainrom
	POP	BC
	LD	B,A
	LD	A,C
	CPL
	LD	E,A			; orginal value
	POP	AF
	PUSH	AF
	PUSH	BC
	CALL	C0583			; call WRSLT mainrom
	POP	BC
	LD	A,C
	CP	B			; location writeable ?
	JR	NZ,J3766		; nope, try next slot
	POP	AF
	DEC	L			; checked XX01H-XX10H ?
	JR	NZ,J3731		; nope, continue
	INC	H
	INC	H
	INC	H
	INC	H			; next Kbyte
	JP	P,J372F			; continue
	POP	HL
	POP	HL			; clean stack
	LD	(SLTWRK+0),A		; slotid RAM
	JR	J377A

J3766:	POP	AF
	POP	HL
	POP	BC
	AND	A			; slot expanded ?
	JP	P,J3773			; nope, next primairy slot
	ADD	A,4
	CP	90H
	JR	C,J372C
J3773:	INC	HL
	INC	A
	DJNZ	J3729
J3777:	JP	J0546			; illegal function call error

J377A:	BIT	6,A
	JR	Z,J3795
	LD	IX,256
	LD	B,20H	; " "
J3784:	CALL	C3AD8
	LD	A,(LINWRK+13)
	AND	A
	JP	NZ,J3BF9
	LD	DE,16
	ADD	IX,DE
	DJNZ	J3784
J3795:	POP	HL
	XOR	A
	LD	DE,03FFH
	SBC	HL,DE
	JR	C,J3804
	INC	H
	PUSH	HL
	LD	B,80H
	LD	C,A
	DEC	A
J37A4:	INC	C
	CALL	C3AEC
	DJNZ	J37A4
	LD	B,H
	XOR	A
	LD	C,A
J37AD:	INC	C
	CALL	C3AEC
	DJNZ	J37AD
	LD	HL,LINWRK
	LD	DE,LINWRK+1
	LD	C,16-1
	LD	(HL),B
	LDIR
	LD	IX,256
	LD	B,32
J37C4:	CALL	C3AD9
	LD	DE,16
	ADD	IX,DE
	DJNZ	J37C4
	LD	HL,SLTWRK+0
	SET	6,(HL)
	POP	HL
	LD	L,B
	PUSH	HL
	CALL	C05CC			; print number
	CALL	C388F
	DEFB	" bytes allocated",13,10
	DEFB	0
	POP	HL
	INC	H
	INC	H
	INC	H
	LD	(LINWRK+0),HL
	LD	HL,LINWRK
	LD	DE,0
	LD	BC,2
	CALL	C3B6A
	POP	HL
	RET

J3804:	XOR	A
	LD	(SLTWRK+0),A
	CALL	C388F
	DEFB	"No RAM disk",13,10
	DEFB	0
	POP	HL
	RET

;	  Subroutine CALL MFILES statement
;	     Inputs  ________________________
;	     Outputs ________________________

C381B:	CALL	C3BFD
	CALL	C05AE			; fresh line to interpreter output
	PUSH	HL
	LD	IX,256
	LD	BC,I2000
J3829:	CALL	C3AD8
	LD	HL,LINWRK
	LD	A,(HL)
	AND	A
	JR	Z,J3861
	INC	C
	PUSH	IX
	PUSH	BC
	LD	B,8
	CALL	C3889			; string to interpreter output
	LD	A,(HL)
	CP	20H	; " "
	JR	Z,J3843
	LD	A,"."
J3843:	RST	18H
	LD	B,3
	CALL	C3889			; string to interpreter output
	LD	A,(LINLEN)
	LD	B,A
	LD	A,(TTYPOS)
	AND	A
	JR	Z,J385E
	ADD	A,0CH	; 12 
	CP	B
	JR	NC,J385B
	LD	A," "
	RST	18H
J385B:	CALL	NC,C05AE		; fresh line to interpreter output
J385E:	POP	BC
	POP	IX
J3861:	LD	DE,16
	ADD	IX,DE
	DJNZ	J3829
	LD	A,C
	OR	A
	JP	Z,J055E			; error 53
	CALL	C05AE			; fresh line to interpreter output
	CALL	C38D7			; fpos (mem device)
	CALL	C05CC			; print number
	CALL	C388F
	DEFB	" bytes free",13,10
	DEFB	0
	POP	HL
	RET


;	  Subroutine string to interpreter output
;	     Inputs  ________________________
;	     Outputs ________________________

C3889:	LD	A,(HL)
	INC	HL
	RST	18H
	DJNZ	C3889
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C388F:	EX	(SP),HL
	LD	A,(HL)
	INC	HL
	EX	(SP),HL
	AND	A
	RET	Z
	RST	18H
	JR	C388F

;	  Subroutine loc (mem device)
;	     Inputs  ________________________
;	     Outputs ________________________

C3898:	PUSH	HL
	POP	IY
	LD	H,(IY+5)
	LD	L,(IY+6)
	JR	J38E6

;	  Subroutine lof (mem device)
;	     Inputs  ________________________
;	     Outputs ________________________

C38A3:	CALL	C3ACF
	PUSH	HL
	POP	IY
	LD	A,(HL)
	CP	01H	; 1 
	LD	HL,(LINWRK+14)
	JR	Z,J38E6
	LD	L,(IY+6)
	JR	J38E6

;	  Subroutine eof (mem device)
;	     Inputs  ________________________
;	     Outputs ________________________

C38B6:	LD	A,(HL)
	CP	01H	; 1 
C38B9:	LD	E,3DH
	JP	NZ,J3C05
	PUSH	HL
	POP	IY
	CALL	C3ACF
	LD	L,(IY+6)
	LD	H,(IY+5)
	LD	DE,(LINWRK+14)
	RST	20H
	LD	HL,-1
	JR	NC,J38E6
	INC	HL
	JR	J38E6


;	  Subroutine fpos (mem device)
;	     Inputs  ________________________
;	     Outputs ________________________

C38D7:	LD	BC,07D01H
	LD	HL,0
J38DD:	CALL	C3AEB
	JR	NZ,J38E3
	INC	H
J38E3:	INC	C
	DJNZ	J38DD
J38E6:	LD	(DAC+2),HL
	LD	A,02H	; 2 
	LD	(VALTYP),A
	RET

;	  Subroutine CALL MKILL statement
;	     Inputs  ________________________
;	     Outputs ________________________

C38EF:	CALL	C3911
	RST	08H
	DEFB	")"
	CALL	C3BF1

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C38F7:	LD	(LINWRK+0),A
	CALL	C3AD9
	LD	A,(LINWRK+11)
	AND	A
	RET	Z
J3902:	LD	C,A
	CALL	C3AEB
	PUSH	AF
	XOR	A
	CALL	C3AEC
	POP	AF
	CP	0FFH
	JR	NZ,J3902
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3911:	RST	08H
	DEFB	"("
	CALL	C3BFD

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3916:	CALL	C05BA			; evaluate filespecification
	RET	Z
	LD	A,D
	CP	0FCH
	JR	NC,J3929
	CP	09H	; 9 
	JR	C,J3929
	PUSH	HL
	CALL	C392C
	POP	HL
	RET	Z
J3929:	JP	J052E			; error 56


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C392C:	LD	HL,PROCNM
	LD	DE,I3941
J3932:	LD	A,(DE)
	CP	(HL)
	JR	NZ,J393C
	INC	HL
	INC	DE
	OR	A
	JR	NZ,J3932
	RET

J393C:	SUB	30H	; "0"
	RET	NZ
	OR	(HL)
	RET

I3941:	DEFB	"MEM0"
	DEFB	0

;	  Subroutine CALL MNAME statement
;	     Inputs  ________________________
;	     Outputs ________________________

C3946:	CALL	C3911
	CALL	C3BF1
	PUSH	IX
	RST	08H
	DEFB	"A"
	RST	08H
	DEFB	"S"
	CALL	C3916
	RST	08H
	DEFB	")"
	CALL	C3B0D
	LD	E,41H
	JP	Z,J3C05
	POP	IX
	CALL	C3AD8
J3964:	PUSH	HL
	LD	DE,LINWRK
	LD	HL,FILNAM
	LD	BC,11
	LDIR
	POP	HL
	JR	J3995

;	  Subroutine open (mem device)
;	     Inputs  ________________________
;	     Outputs ________________________

C3973:	CALL	C3BFD
	CALL	C3B0D
	PUSH	AF
	LD	A,E
	CP	04H	; 4 
	JP	Z,J0534			; error 58
	CP	02H	; 2 
	JR	Z,J3998
	CP	08H	; 8 
	JR	Z,J39D1
	POP	AF
	JR	NZ,J39D2
	OR	A
	JR	NZ,J39D6
	CALL	C3BD6

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3991:	DEC	A
	LD	(LINWRK+13),A
J3995:	JP	C3AD9

J3998:	POP	AF
	JR	NZ,J39A1
	OR	A
	JR	NZ,J39D6
	CALL	C38F7
J39A1:	LD	IX,256
	LD	B,20H	; " "
J39A7:	CALL	C3AD8
	LD	A,(LINWRK+0)
	AND	A
	JR	Z,J39BC
	LD	DE,16
	ADD	IX,DE
	DJNZ	J39A7
	LD	E,43H
	JP	J3C05

J39BC:	LD	E,02H	; 2 
	CALL	C3BD6
	LD	HL,LINWRK
	LD	B,10H	; 16 
J39C6:	LD	(HL),A
	INC	HL
	DJNZ	J39C6
	DEC	A
	LD	(LINWRK+13),A
	JP	J3964

J39D1:	POP	AF
J39D2:	JP	NZ,J055E		; error 53
	OR	A
J39D6:	JP	NZ,J053A		; error 54
	CALL	C3BD6
	LD	HL,(LINWRK+14)
	LD	(IY+5),H
	LD	(IY+6),L
	LD	L,A
	LD	(LINWRK+14),HL
	CALL	C3991
	LD	A,(LINWRK+11)
	AND	A
	RET	Z
	LD	A,(LINWRK+12)
	LD	(IY+7),A
	JP	C3B51

;	  Subroutine close (mem device)
;	     Inputs  ________________________
;	     Outputs ________________________

C39FA:	CALL	C3ACF
	XOR	A
	LD	(LINWRK+13),A
	CALL	C3AD9
	LD	A,(HL)
	CP	01H	; 1 
	RET	Z
	PUSH	HL
	POP	IY
	LD	A,(IY+6)
	AND	A
	RET	Z
	CALL	C3B52
	LD	HL,(LINWRK+14)
	LD	L,(IY+6)
	LD	(LINWRK+14),HL
	JR	J3A7A

;	  Subroutine seq output (mem device)
;	     Inputs  ________________________
;	     Outputs ________________________

C3A1E:	PUSH	HL
	POP	IY
	LD	A,(IY+6)
	AND	A
	JR	NZ,J3A5D
	PUSH	HL
	PUSH	BC
	CALL	C3ACF
	LD	BC,07D01H
J3A2F:	CALL	C3AEB
	JR	Z,J3A3C
	INC	C
	DJNZ	J3A2F
	LD	E,42H
	JP	J3C05

J3A3C:	LD	A,(LINWRK+11)
	AND	A
	LD	A,C
	JR	NZ,J3A46
	LD	(LINWRK+11),A
J3A46:	LD	C,(IY+7)
	CALL	NZ,C3AEC
	LD	(LINWRK+12),A
	LD	(IY+7),A
	LD	C,A
	LD	A,0FFH
	CALL	C3AEC
	CALL	C3AD9
	POP	BC
	POP	HL
J3A5D:	LD	DE,9
	ADD	HL,DE
	LD	E,(IY+6)
	ADD	HL,DE
	LD	(HL),C
	INC	(IY+6)
	RET	NZ
	CALL	C3B52
	INC	(IY+5)
	PUSH	IY
	POP	HL
	CALL	C3ACF
	LD	HL,LINWRK+15
	INC	(HL)
J3A7A:	JR	C3AD9

;	  Subroutine seq input (mem device)
;	     Inputs  ________________________
;	     Outputs ________________________

C3A7C:	PUSH	HL
	PUSH	HL
	POP	IY
	CALL	C3ACF
	LD	L,(IY+6)
	LD	H,(IY+5)
	LD	DE,(LINWRK+14)
	RST	20H
	POP	HL
	CCF
	RET	C
	LD	A,(IY+3)
	AND	A
	LD	(IY+3),00H
	RET	NZ
	LD	A,(IY+6)
	AND	A
	JR	Z,J3AAF
	LD	BC,9
	ADD	HL,BC
	LD	C,A
	ADD	HL,BC
	LD	A,(HL)
	INC	(IY+6)
	RET	NZ
	INC	(IY+5)
	RET

J3AAF:	LD	A,(IY+7)
	AND	A
	LD	C,A
	LD	A,(LINWRK+11)
	CALL	NZ,C3AEB
	LD	(IY+7),A
	CALL	C3B51
	LD	A,(IY+9)
	INC	(IY+6)
	AND	A
	RET

;	  Subroutine putback (mem device)
;	     Inputs  ________________________
;	     Outputs ________________________

C3AC8:	PUSH	HL
	POP	IY
	LD	(IY+3),C
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3ACF:	INC	HL
	LD	C,(HL)
	INC	HL
	LD	B,(HL)
	PUSH	BC
	POP	IX
	DEC	HL
	DEC	HL

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3AD8:	DEFB	0F6H

C3AD9:	SCF
	PUSH	HL
	PUSH	DE
	PUSH	BC
	PUSH	IX
	POP	HL
	LD	DE,LINWRK
	JR	NC,J3AE6
	EX	DE,HL
J3AE6:	LD	BC,16
	JR	J3B02


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3AEB:	DEFB	0F6H

C3AEC:	SCF
	PUSH	HL
	PUSH	DE
	PUSH	BC
	PUSH	AF
	LD	HL,127
	LD	B,00H
	ADD	HL,BC
	LD	DE,LINWRK+16
	POP	AF
	JR	NC,J3AFF
	EX	DE,HL
	LD	(HL),A
J3AFF:	LD	BC,1
J3B02:	CALL	C3B6A
	POP	BC
	POP	DE
	POP	HL
	LD	A,(LINWRK+16)
	AND	A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3B0D:	PUSH	HL
	PUSH	DE
	LD	DE,FILNAM
	LD	B,0BH	; 11 
J3B14:	LD	A,(DE)
	CP	61H	; "a"
	JR	C,J3B20
	CP	7BH	; "{"
	JR	NC,J3B20
	AND	0DFH
	LD	(DE),A
J3B20:	INC	DE
	DJNZ	J3B14
	LD	IX,256
	LD	C,20H	; " "
J3B29:	CALL	C3AD8
	LD	HL,LINWRK
	LD	A,(HL)
	AND	A
	JR	NZ,J3B3E
J3B33:	LD	DE,16
	ADD	IX,DE
	DEC	C
	JR	NZ,J3B29
	INC	C
	JR	J3B4B

J3B3E:	LD	DE,FILNAM
	LD	B,0BH	; 11 
J3B43:	LD	A,(DE)
	CP	(HL)
	JR	NZ,J3B33
	INC	DE
	INC	HL
	DJNZ	J3B43
J3B4B:	POP	DE
	POP	HL
	LD	A,(LINWRK+13)
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3B51:	DEFB	0F6H

C3B52:	SCF
	PUSH	AF
	PUSH	IY
	POP	HL
	LD	BC,9
	ADD	HL,BC
	LD	E,00H
	LD	A,(IY+7)
	ADD	A,02H	; 2 
	LD	D,A
	POP	AF
	JR	C,J3B67
	EX	DE,HL
J3B67:	LD	BC,256

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3B6A:	LD	A,H
	AND	A
	JP	P,J3B70
	LD	A,D
J3B70:	CP	40H	; "@"
	EXX
	LD	HL,D.FFFF
	LD	A,(SLTWRK+0)
	LD	D,A
	JR	C,J3BB7
	RRCA
	RRCA
	AND	0C0H
	LD	C,A
	RRCA
	RRCA
	RRCA
	RRCA
	OR	C
	LD	C,A
	IN	A,(PPI_PORT)
	LD	B,A
	AND	33H	; "3"
	OR	C
	LD	E,A
	DI
	OUT	(PPI_PORT),A
	LD	A,D
	AND	0CH	; 12 
	LD	D,A
	LD	A,(HL)
	CPL
	LD	C,A
	AND	0F3H
	OR	D
	LD	(HL),A
	LD	A,E
	AND	3FH
	LD	D,A
	LD	A,B
	AND	0C0H
	OR	D
	EI
	OUT	(PPI_PORT),A
	EXX
	LDIR
	EXX
	LD	A,E
	DI
	OUT	(PPI_PORT),A
	LD	A,C
	LD	(HL),A
	LD	A,B
	EI
	OUT	(PPI_PORT),A
	EXX
	RET

J3BB7:	RRCA
	RRCA
	AND	0C0H
	LD	C,A
	LD	A,D
	AND	03H	; 3 
	OR	C
	LD	C,A
	IN	A,(PPI_PORT)
	LD	B,A
	AND	3CH	; "<"
	OR	C
	LD	E,A
	LD	A,D
	RRCA
	RRCA
	AND	03H	; 3 
	LD	D,A
	LD	A,E
	DI
	CALL	C51D7
	EI
	EXX
	RET

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3BD6:	PUSH	HL
	POP	IY
	PUSH	IX
	POP	BC
	LD	(IY+1),C
	LD	(IY+2),B
	XOR	A
	LD	(IY+3),A
	LD	(IY+5),A
	LD	(IY+6),A
	LD	(PTRFIL),HL
	LD	(HL),E
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3BF1:	CALL	C3B0D
	JP	NZ,J055E		; error 53
	AND	A
	RET	Z
J3BF9:	LD	E,40H
	JR	J3C05


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3BFD:	LD	A,(SLTWRK+0)
	BIT	6,A
	RET	NZ
	LD	E,46H
J3C05:	XOR	A
	LD	(NLONLY),A
	PUSH	DE
	CALL	C05C0			; close i/o channel
	POP	DE
	JP	J05C6			; BASIC error

;	Subroutine	extended errorhandler
;	Inputs		HL = errortext pointer, E=errorcode
;	Outputs		HL = errortext pointer, E=errorcode

I3C11:	LD	BC,I3C34
J3C14:	LD	A,(BC)
	INC	BC
	OR	A
	RET	Z
	SUB	E
	JR	Z,J3C1F
	INC	BC
	INC	BC
	JR	J3C14

J3C1F:	LD	L,C
	LD	H,B
	LD	E,(HL)
	INC	HL
	LD	D,(HL)
	LD	HL,BUF
	PUSH	HL
	LD	(HL),A
J3C29:	INC	DE
	INC	HL
	LD	A,(DE)
	LD	(HL),A
	OR	A
	JR	NZ,J3C29
	POP	HL
	LD	E,1
	RET

I3C34:	DEFB	03DH
	DEFW	I3C47-1
	DEFB	040H
	DEFW	I3C55-1
	DEFB	041H
	DEFW	I3C65-1
	DEFB	042H
	DEFW	I3C79-1
	DEFB	43H
	DEFW	I3C87-1
	DEFB	46H
	DEFW	I3C96-1
	DEFB	0

I3C47:	DEFB	"Bad file mode"
	DEFB	0

I3C55:	DEFB	"File still open"
	DEFB	0

I3C65:	DEFB	"File already exists"
	DEFB	0

I3C79:	DEFB	"RAM disk full"
	DEFB	0

I3C87:	DEFB	"Too many files"
	DEFB	0

I3C96:	DEFB	"RAM disk offline"
	DEFB	0

	IF	CNTRY = 0
J3CA7:	LD	HL,KANAMD
	INC	(HL)
	DEC	(HL)			; ANSI layout ?
	LD	B,8
	LD	DE,I3F4E
	LD	HL,I3F3E
	JR	Z,J3CB9			; yep,
	LD	HL,I3F46
J3CB9:	CP	(HL)
	JR	Z,J3CC6
	INC	HL
	INC	DE
J3CBE:	DJNZ	J3CB9
J3CC0:	CALL	C3ED1			; put in keyboardbuffer
	JP	J3DC7			; end vowel

J3CC6:	LD	A,(DE)
	JR	J3CC0

;	  Subroutine PUTCHR
;	     Inputs  ________________________
;	     Outputs ________________________

J3CC9:	PUSH	AF
	CALL	C3EE7			; generate keyclick
	POP	AF
	JR	Z,J3CD5			; generated by a functionkey, put in keyboardbuffer and quit
	LD	HL,MODE
	BIT	0,(HL)			; in SHIFT KANA mode ?
J3CD5:	JP	Z,C3ED1			; nope, put in keyboardbuffer and quit
	CP	"A"
	JR	C,J3CA7
	CP	"Z"+1
	JR	C,J3CEC			; upcase letter, flag SHIFT KANA upcase
	CP	"a"
	JR	C,J3CA7
	CP	"z"+1
	JR	NC,J3CA7
	RES	7,(HL)			; lowercase letter, flag SHIFT KANA lowercase
	JR	J3CEE

J3CEC:	SET	7,(HL)
J3CEE:	CP	"a"			; upcase letter ?
	JR	C,J3CF4
	SUB	20H			; nope, make uppercase
J3CF4:	LD	D,A			; save character
	CP	"X"
	JR	Z,J3D15
	LD	A,(CHRCNT)
	OR	A			; already in a vowel ?
	JR	NZ,J3D1A		; yep,
	LD	A,D
	CALL	C3E2F
	LD	A,D
	JP	NZ,C3EB6
	CALL	C3E20
	CP	80H			; >= 80H ?
	RET	NC			; yep, quit
	LD	A,D
	LD	(ROMA+0),A
	LD	A,1
	JR	J3D55			; 1 character in vowel

J3D15:	LD	A,0B0H
	JP	J3CC0

J3D1A:	DEC	A			; has 1 character already ?
	LD	A,D
	JR	NZ,J3D57		; nope, 2 characters
J3D1E:	LD	E,A
	CALL	C3E2F
	LD	A,(ROMA+0)
	JR	Z,J3D2C
	CALL	C3E3D
	JR	J3D38			; end vowel

J3D2C:	CP	E
	JR	NZ,J3D3F
	CP	"N"
	JR	NZ,J3D3B
	LD	A,0FDH
	CALL	C3EBC			; put in keyboardbuffer (case conversion)
J3D38:	JP	J3DC7			; end vowel

J3D3B:	LD	A,8FH
	JR	J3D4D			; put in keyboardbuffer (case conversion)

J3D3F:	CP	"N"
	LD	A,E
	JR	NZ,J3D50
	CP	"Y"
	JR	Z,J3D50
	LD	(ROMA+0),A
	LD	A,0FDH
J3D4D:	JP	C3EBC			; put in keyboardbuffer (case conversion)

J3D50:	LD	(ROMA+1),A
	LD	A,2			; 2 characters in vowel
J3D55:	JR	J3DC8

J3D57:	LD	HL,ROMA
	LD	D,(HL)
	INC	HL
	LD	B,(HL)
	LD	C,A
	PUSH	BC
	CALL	C3E2F
	POP	BC
	JR	NZ,J3D72
	LD	A,B
	LD	(ROMA+0),A
	LD	A,1
	LD	(CHRCNT),A
	LD	A,C
	JP	J3D1E

J3D72:	LD	A,B
	CP	"Y"
	JR	NZ,J3D9D
	LD	A,D
	CP	"C"
	JR	Z,J3D90
	CP	"L"
	JR	Z,J3DC1
	CP	"F"
	JR	Z,J3D99
	CP	"W"
	JR	Z,J3DF2
	LD	E,49H	; "I"
	PUSH	BC
	CALL	C3E3D
	JR	J3D96

J3D90:	LD	A,0E1H
J3D92:	PUSH	BC
	CALL	C3EBC			; put in keyboardbuffer (case conversion)
J3D96:	POP	BC
	JR	J3DC1

J3D99:	LD	A,0ECH
	JR	J3D92

J3D9D:	LD	A,B
	CP	"H"
	JR	NZ,J3DDC
	LD	A,D
	CP	"C"
	JR	Z,J3DB7
	CP	"D"
	JR	Z,J3DCC
	CP	"T"
	JR	Z,J3DD5
	CP	"S"
	JR	NZ,J3DF2
	LD	A,9CH
	JR	J3DB9

J3DB7:	LD	A,0E1H
J3DB9:	CALL	C3EBC			; put in keyboardbuffer (case conversion)
J3DBC:	LD	A,C
	CP	"I"
	JR	Z,J3DC7			; end vowel
J3DC1:	LD	A,"J"
	LD	E,C
	CALL	C3EB9
J3DC7:	XOR	A
J3DC8:	LD	(CHRCNT),A
	RET

J3DCC:	LD	A,0E3H
	CALL	C3EBC			; put in keyboardbuffer (case conversion)
	LD	A,0DEH
	JR	J3DD7

J3DD5:	LD	A,0E3H
J3DD7:	CALL	C3EBC			; put in keyboardbuffer (case conversion)
	JR	J3DC1

J3DDC:	LD	A,D
	CP	"T"
	JR	NZ,J3DF2
	LD	A,B
	CP	"S"
	JR	NZ,J3DF2
	LD	A,C
	CP	"U"
	JR	NZ,J3DF2
	LD	A,0E2H
	CALL	C3EBC			; put in keyboardbuffer (case conversion)
	JR	J3DC7			; end vowel

J3DF2:	LD	A,B
	LD	E,C
	CALL	C3E3D
	JR	J3DC7			; end vowel


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3DF9:	CALL	C3E29
	LD	B,A
	ADD	A,A
	ADD	A,A
	ADD	A,B
	LD	D,A
	LD	A,E
	CALL	C3E2F
	ADD	A,D
	LD	L,A
	LD	A,D
	CP	0FH	; 15 
	JR	NC,J3E10
	LD	A,L
	SUB	70H
	RET

J3E10:	CP	23H	; "#"
	JR	NC,J3E18
	LD	A,L
	SUB	30H	; "0"
	RET

J3E18:	LD	A,L
	SUB	23H	; "#"
	LD	BC,I3F71
	JR	J3E23


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3E20:	LD	BC,I3F56-040H
J3E23:	LD	L,A
	LD	H,0
	ADD	HL,BC
	LD	A,(HL)
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3E29:	CALL	C3E20
	AND	0FH
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3E2F:	CALL	C3E20
	SUB	14H
	CP	1			; is it 014H ?
	JR	C,J3E3B
	CP	06H	; 6 
	RET	C
J3E3B:	XOR	A
	RET


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3E3D:	LD	D,A
	CALL	C3E20
	CP	0BH			; >= 0BH ?
	JR	NC,J3E48		; yep,
	LD	A,D
	JR	J3E8A

J3E48:	CP	21H	; "!"
	JR	C,J3E58
	CP	26H	; "&"
	JR	NC,J3E58
	LD	A,D
	CALL	C3EB9

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3E54:	LD	A,0DEH
	JR	C3EBC			; put in keyboardbuffer (case conversion)

J3E58:	LD	A,D
	CP	"P"
	JR	NZ,J3E67
	LD	A,"H"
	CALL	C3EB9
	LD	A,0DFH
	JP	C3EBC			; put in keyboardbuffer (case conversion)

J3E67:	CP	"J"
	JR	NZ,J3E79
	LD	A,9CH
	PUSH	DE
	CALL	C3EBC			; put in keyboardbuffer (case conversion)
	CALL	C3E54
	POP	DE
	LD	C,E
	JP	J3DBC

J3E79:	CP	"F"
	JR	NZ,J3E8C
	LD	A,0ECH
	PUSH	DE
	CALL	C3EBC			; put in keyboardbuffer (case conversion)
J3E83:	POP	DE
	LD	A,E
	CP	"U"
	RET	Z
J3E88:	LD	A,"L"
J3E8A:	JR	C3EB9

J3E8C:	CP	"Y"
	JR	NZ,J3E96
	CALL	C3EB9
	LD	A,E
	JR	J3EA2

J3E96:	CP	"W"
	JR	NZ,J3EA7
	CALL	C3EB9
	LD	A,E
	CP	"I"
	JR	Z,J3E88
J3EA2:	CP	"E"
	RET	NZ
	JR	J3E88

J3EA7:	CP	"V"
	JR	NZ,J3EB7
	LD	A,55H	; "U"
	PUSH	DE
	CALL	C3EB6
	CALL	C3E54
	JR	J3E83


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3EB6:	LD	E,A
J3EB7:	LD	A,"Q"

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

C3EB9:	CALL	C3DF9

;	  Subroutine put in keyboardbuffer (case conversion)
;	     Inputs  ________________________
;	     Outputs ________________________

C3EBC:	LD	HL,MODE
	BIT	7,(HL)			; upcase SHIFT KANA ?
	JR	Z,C3ED1			; nope, put in keyboardbuffer and quit
	CP	0E0H
	JR	C,J3ECB
	SUB	20H
	JR	C3ED1			; put in keyboardbuffer and quit

J3ECB:	CP	0A0H
	JR	NC,C3ED1		; put in keyboardbuffer and quit
	ADD	A,20H
					; put in keyboardbuffer and quit

;	  Subroutine put in keyboardbuffer
;	     Inputs  ________________________
;	     Outputs ________________________

C3ED1:	LD	HL,(PUTPNT)
	LD	(HL),A
	INC	HL
	LD	A,L
	CP	(KEYBUF+40) AND 255
	JR	NZ,J3EDE
	LD	HL,KEYBUF
J3EDE:	LD	A,(GETPNT)
	CP	L
	RET	Z
	LD	(PUTPNT),HL
	RET


;	  Subroutine generate keyclick
;	     Inputs  ________________________
;	     Outputs ________________________

C3EE7:	LD	A,(CLIKSW)
	AND	A
	RET	Z
	LD	A,(CLIKFL)
	AND	A
	RET	NZ
	LD	A,0FH
	LD	(CLIKFL),A
	DI
	OUT	(PPI_PORT+3),A
	LD	A,10
J3EFB:	DEC	A
	JR	NZ,J3EFB
	LD	A,0EH
	OUT	(PPI_PORT+3),A
	EI
	RET

;	  Subroutine KYKLOK
;	     Inputs  ________________________
;	     Outputs ________________________

J3F04:
	LD	HL,MODE
	BIT	0,(HL)			; in SHIFT KANA mode ?
	JR	Z,J3F10			; nope,
	RES	0,(HL)			; leave SHIFT KANA mode
	XOR	A
	JR	J3F29			; KANA led off

J3F10:	LD	A,(KANAST)
	INC	A			; in KANA mode ?
	JR	Z,J3F26			; yep, leave KANA mode and KANA led off
I3F16:	LD	A,(NEWKEY+6)
	RRCA				; SHIFT also pressed ?
	JR	C,J3F24			; nope, switch to KANA mode and KANA led on
	XOR	A
	LD	(CHRCNT),A
	SET	0,(HL)			; switch to SHIFT KANA mode
	JR	J3F29			; KANA led off

J3F24:	LD	A,0FFH
J3F26:	LD	(KANAST),A
J3F29:	PUSH	AF
	LD	A,0FH
	OUT	(PSG_PORT),A
	IN	A,(PSG_PORT+2)
	AND	7FH
	LD	B,A
	POP	AF
	OR	A
	LD	A,80H
	JR	Z,J3F3A
	XOR	A
J3F3A:	OR	B
	OUT	(PSG_PORT+1),A
	RET

; ANSI layout

I3F3E:	DEFB	"*:]_?>{}"

; JIS layout

I3F46:	DEFB	"\\@>?[<{}"

; replacement table

I3F4E:	DEFB	0B0H,0DEH,0A1H,0A5H,0DFH,0A4H,0A2H,0A3H

I3F56:	DEFB	00AH	; unused entry
	DEFB	015H	; A
	DEFB	025H	; B
	DEFB	028H	; C
	DEFB	023H	; D
	DEFB	018H	; E
	DEFB	040H	; F
	DEFB	021H	; G
	DEFB	005H	; H
	DEFB	016H	; I
	DEFB	04BH	; J
	DEFB	001H	; K
	DEFB	00AH	; L
	DEFB	006H	; M
	DEFB	004H	; N
	DEFB	019H	; O
	DEFB	028H	; P
	DEFB	080H	; Q
	DEFB	008H	; R
	DEFB	002H	; S
	DEFB	003H	; T
	DEFB	017H	; U
	DEFB	040H	; V
	DEFB	049H	; W
	DEFB	08CH	; X
	DEFB	047H	; Y
	DEFB	022H	; Z

I3F71:	DEFB	0FDH,0F4H,092H,0F5H,092H,0F6H,0F7H,0F8H
	DEFB	0F9H,0FAH,0FBH,0FCH,093H,093H,093H,086H
	DEFB	087H,088H,089H,08AH,08BH,08CH,088H,08DH
	DEFB	08AH,08EH

	ENDIF

	IF	CNTRY != 0

C3CA7:	CALL	C1947
	DEC	HL
	LD	C,(HL)
	DEC	HL
	LD	B,(HL)
	DEC	HL
	BIT	4,(HL)
	RET	NZ
J3CB2:	LD	DE,-5
	ADD	HL,DE
	LD	D,(HL)
	LD	(HL),B
	INC	HL
	LD	E,(HL)
	LD	(HL),C
	LD	BC,5
J3CBE:	ADD	HL,BC
	LD	(HL),D
J3CC0:	INC	HL
	LD	(HL),E
	RET

C3CC3:
	LD	(AVCSAV),A
	XOR	A
	OUT	(AV_PORT+1),A
J3CC9:
	LD	(D.FFF8),A
J3CCC:
	RET

C3CCD:	RST	28H
J3CCE:	CP	16
J3CD0:	RET	NC
	PUSH	AF
	RRCA
	RRCA
	AND	03H
	LD	E,A
	LD	A,0FCH
	CALL	C3CE1
	POP	AF
	AND	03H
	SCF
	RET

C3CE1:	PUSH	HL
J3CE2:	LD	HL,D.FFF8
	AND	(HL)
	OR	E
	LD	(HL),A
	OUT	(AV_PORT+1),A
	POP	HL
	RET

J3F04:
	RET

	ENDIF


	IFDEF	FS-5500
	
C3F8B:	LD	D,48
J3F8D:	DEC	D
	JR	NZ,J3F8D
	JP	C3585

	ENDIF


	IF	CNTRY != 0
	
; by Fabio - contagem da RAM (copiado da subrom do 2+)
;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

RAM_COUNT:								; contagem da RAM
	ld		hl, 0
	ld		de, EXPTBL						; faz um loop para cada slot
	ld		b, 0
.l1:
	ld		a, (de)
	ld		c, a
.l2:
	ld		a, c
	or		b
	call	RAM_COUNT_SLT				; chama funcao que procura e conta RAM no slot passado em A
	bit		7, c
	jr z, 	.l3
	ld		a, c
	add		a, 4
	ld		c, a
	bit		4, c
	jr z,	.l2
.l3:
	inc		de
	inc		b
	ld		a, b
	cp		4
	jr c,	.l1
	push	hl
	ld		h, $40
	ld		a, (EXPTBL+0)
	call	C02BD						; setar slots
	pop		hl
	ret


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

RAM_COUNT_SLT:							; procura e conta RAM no slot passado em A
	push	bc
	push	de
	push	hl
	ld		h, $40
	call	C02BD						; setar slots
	CALL	FIND_COUNT_RAM
	pop		hl
	jr c,	.l2
	ld		c, a
	ld		b, 0
	and		a
	jr nz,	.l1
	inc		b
.l1:
	add		hl, bc
.l2:
	pop		de
	pop		bc
	ret


;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

FIND_COUNT_RAM:							; Procura RAM nas paginas da mapper
	ld		hl, $4000
	xor		a
	out		(MAPPER_PORT+1), a
	LD		(hl), $AA
	inc		a
	out		(MAPPER_PORT+1), a
	ld		(hl), $55
	dec		a
	out		(MAPPER_PORT+1), a
	ld		a, (hl)
	cp		$AA
	scf
	jr nz,	.l4
	ld		b, 0
.l1:
	ld		a, b
	out		(MAPPER_PORT+1), a
	ld		(hl), $AA
	inc		b
	jr nz,	.l1
.l2:
	ld		a, b
	out		(MAPPER_PORT+1), a
	inc		b
	ld		a, (hl)
	cp		$AA
	jr nz,	.l3
	ld		a, $55
	ld		(hl), a
	cp		(hl)
	jr z,	.l2
.l3:
	dec		b
	and		a
.l4:
	ld		a, 2
	out		(MAPPER_PORT+1), a
	ld		a, b
	ret

; Converter numero (HL) em string base 10 (HL aponta para buffer)
;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

NUM_TO_STR:
	ld		ix, BUF
	ld		c, 0
	ld		DE, $D8F0
	call	DIG_TO_STR
	ld		DE, $FC18
	call	DIG_TO_STR
	ld		DE, $FF9C
	call	DIG_TO_STR
	ld		DE, $FFF6
	call	DIG_TO_STR
	ld		a, l
	add		a, '0'
	ld		(ix), a
	ld		(ix+1), 0
	ld		hl, BUF
	ret

;	  Subroutine __________________________
;	     Inputs  ________________________
;	     Outputs ________________________

DIG_TO_STR:
	ld		a, $FF
.l1:
	add		hl, de
	inc		a
	jr c,	.l1
	sbc		hl ,de
	bit		0, c
	jr nz,	.l2
	and		a
	jr nz,	.l3
	ld		a, ' '
	ld		(ix), a
	inc		ix
	ret
.l3:
	set		0, c
.l2:
	add		a, 30h	; "0"
	ld		(ix), a
	inc		ix
	ret

	ENDIF

	DISPLAY	"Temos ", $4000-$, " bytes sobrando"

	DEFS	04000H-$,0
